-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add User and Token with Ash Authentication
- Loading branch information
Showing
17 changed files
with
348 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule PetalStackTutorial.Accounts do | ||
use Ash.Domain | ||
|
||
resources do | ||
resource PetalStackTutorial.Accounts.Token | ||
resource PetalStackTutorial.Accounts.User | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule PetalStackTutorial.Accounts.Secrets do | ||
use AshAuthentication.Secret | ||
|
||
def secret_for([:authentication, :tokens, :signing_secret], PetalStackTutorial.Accounts.User, _) do | ||
case Application.fetch_env(:petal_stack_tutorial, PetalStackTutorialWeb.Endpoint) do | ||
{:ok, endpoint_config} -> | ||
Keyword.fetch(endpoint_config, :secret_key_base) | ||
|
||
:error -> | ||
:error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule PetalStackTutorial.Accounts.Token do | ||
use Ash.Resource, | ||
domain: PetalStackTutorial.Accounts, | ||
data_layer: AshPostgres.DataLayer, | ||
extensions: [AshAuthentication.TokenResource] | ||
|
||
postgres do | ||
table "tokens" | ||
repo PetalStackTutorial.Repo | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule PetalStackTutorial.Accounts.User do | ||
use Ash.Resource, | ||
domain: PetalStackTutorial.Accounts, | ||
data_layer: AshPostgres.DataLayer, | ||
extensions: [AshAuthentication] | ||
|
||
postgres do | ||
table "users" | ||
repo PetalStackTutorial.Repo | ||
end | ||
|
||
attributes do | ||
uuid_primary_key :id | ||
|
||
attribute :email, :ci_string do | ||
allow_nil? false | ||
public? true | ||
end | ||
|
||
attribute :hashed_password, :string do | ||
allow_nil? false | ||
sensitive? true | ||
end | ||
end | ||
|
||
authentication do | ||
strategies do | ||
password :password do | ||
identity_field :email | ||
end | ||
end | ||
|
||
tokens do | ||
enabled? true | ||
token_resource PetalStackTutorial.Accounts.Token | ||
signing_secret PetalStackTutorial.Accounts.Secrets | ||
end | ||
end | ||
|
||
identities do | ||
identity :unique_email, [:email] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
lib/petal_stack_tutorial_web/controllers/auth_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule PetalStackTutorialWeb.AuthController do | ||
use PetalStackTutorialWeb, :controller | ||
use AshAuthentication.Phoenix.Controller | ||
|
||
def success(conn, _activity, user, _token) do | ||
return_to = get_session(conn, :return_to, ~p"/") | ||
|
||
conn | ||
|> delete_session(:return_to) | ||
|> store_in_session(user) | ||
|> assign(:current_user, user) | ||
|> redirect(to: return_to) | ||
end | ||
|
||
def failure(conn, _activity, _reason) do | ||
conn | ||
|> put_flash(:error, "Incorrect email or password") | ||
|> redirect(to: ~p"/sign-in") | ||
end | ||
|
||
def sign_out(conn, _params) do | ||
return_to = get_session(conn, :return_to, ~p"/") | ||
|
||
conn | ||
|> clear_session() | ||
|> redirect(to: return_to) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.