Skip to content

Commit da74838

Browse files
committed
add User and Token with Ash Authentication
1 parent f58f674 commit da74838

File tree

17 files changed

+348
-10
lines changed

17 files changed

+348
-10
lines changed

.formatter.exs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
[
2-
import_deps: [:ecto, :ecto_sql, :phoenix, :ash, :ash_phoenix, :ash_postgres],
2+
import_deps: [
3+
:ecto,
4+
:ecto_sql,
5+
:phoenix,
6+
:ash,
7+
:ash_phoenix,
8+
:ash_postgres,
9+
:ash_authentication,
10+
:ash_authentication_phoenix
11+
],
312
subdirectories: ["priv/*/migrations"],
413
plugins: [Phoenix.LiveView.HTMLFormatter],
514
inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "priv/*/seeds.exs"]

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,15 @@ Blog.destroy_post!(updated_post)
523523
The implementation of these actions are only defined once in the `actions` block of the Post resource. This lays the foundation for deriving more functionality from these actions. We could also create other resources and domains.
524524

525525
In further sections I'll discuss
526-
- User and Login with `AshAuthentication`
527526
- Integrating Ash models with LiveView forms
528-
- REST API with `AshJsonApi`
529-
- GraphQL API with `AshGraphql`
527+
- User and Login with [`AshAuthentication`](https://hexdocs.pm/ash_authentication_phoenix/get-started.html)
528+
- REST API with [`AshJsonApi`](https://ash-hq.org/docs/guides/ash_json_api/latest/tutorials/getting-started-with-ash-json-api)
529+
- GraphQL API with [`AshGraphql`](https://ash-hq.org/docs/guides/ash_graphql/latest/tutorials/getting-started-with-graphql)
530530

531-
### Section 4: TBD
531+
### Section 4: Users with Ash Authentication
532+
533+
I follower [this tutorial](https://hexdocs.pm/ash_authentication_phoenix/get-started.html) for adding users to a Phoenix app with Ash Authentication and Ash Phoenix Authentication. I did not write addtional code.
534+
535+
### Section 5: REST API with with Ash JSON API
536+
537+
### Section 6: GraphQL API with Ash GraphQL

assets/tailwind.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ module.exports = {
99
content: [
1010
"./js/**/*.js",
1111
"../lib/petal_stack_tutorial_web.ex",
12-
"../lib/petal_stack_tutorial_web/**/*.*ex"
12+
"../lib/petal_stack_tutorial_web/**/*.*ex",
13+
"../deps/ash_authentication_phoenix/**/*.*ex",
1314
],
1415
theme: {
1516
extend: {

config/config.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import Config
99

1010
config :petal_stack_tutorial,
11-
ash_domains: [PetalStackTutorial.Blog]
11+
ash_domains: [PetalStackTutorial.Accounts, PetalStackTutorial.Blog],
12+
ash_apis: [PetalStackTutorial.Accounts]
1213

1314
# Configures the endpoint
1415
config :petal_stack_tutorial, PetalStackTutorialWeb.Endpoint,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule PetalStackTutorial.Accounts do
2+
use Ash.Domain
3+
4+
resources do
5+
resource PetalStackTutorial.Accounts.Token
6+
resource PetalStackTutorial.Accounts.User
7+
end
8+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule PetalStackTutorial.Accounts.Secrets do
2+
use AshAuthentication.Secret
3+
4+
def secret_for([:authentication, :tokens, :signing_secret], PetalStackTutorial.Accounts.User, _) do
5+
case Application.fetch_env(:petal_stack_tutorial, PetalStackTutorialWeb.Endpoint) do
6+
{:ok, endpoint_config} ->
7+
Keyword.fetch(endpoint_config, :secret_key_base)
8+
9+
:error ->
10+
:error
11+
end
12+
end
13+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule PetalStackTutorial.Accounts.Token do
2+
use Ash.Resource,
3+
domain: PetalStackTutorial.Accounts,
4+
data_layer: AshPostgres.DataLayer,
5+
extensions: [AshAuthentication.TokenResource]
6+
7+
postgres do
8+
table "tokens"
9+
repo PetalStackTutorial.Repo
10+
end
11+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule PetalStackTutorial.Accounts.User do
2+
use Ash.Resource,
3+
domain: PetalStackTutorial.Accounts,
4+
data_layer: AshPostgres.DataLayer,
5+
extensions: [AshAuthentication]
6+
7+
postgres do
8+
table "users"
9+
repo PetalStackTutorial.Repo
10+
end
11+
12+
attributes do
13+
uuid_primary_key :id
14+
15+
attribute :email, :ci_string do
16+
allow_nil? false
17+
public? true
18+
end
19+
20+
attribute :hashed_password, :string do
21+
allow_nil? false
22+
sensitive? true
23+
end
24+
end
25+
26+
authentication do
27+
strategies do
28+
password :password do
29+
identity_field :email
30+
end
31+
end
32+
33+
tokens do
34+
enabled? true
35+
token_resource PetalStackTutorial.Accounts.Token
36+
signing_secret PetalStackTutorial.Accounts.Secrets
37+
end
38+
end
39+
40+
identities do
41+
identity :unique_email, [:email]
42+
end
43+
end

lib/petal_stack_tutorial/application.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ defmodule PetalStackTutorial.Application do
1818
# Start a worker by calling: PetalStackTutorial.Worker.start_link(arg)
1919
# {PetalStackTutorial.Worker, arg},
2020
# Start to serve requests, typically the last entry
21-
PetalStackTutorialWeb.Endpoint
21+
PetalStackTutorialWeb.Endpoint,
22+
{AshAuthentication.Supervisor, otp_app: :petal_stack_tutorial}
2223
]
2324

2425
# See https://hexdocs.pm/elixir/Supervisor.html

lib/petal_stack_tutorial_web.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule PetalStackTutorialWeb do
2121

2222
def router do
2323
quote do
24-
use Phoenix.Router, helpers: false
24+
use Phoenix.Router, helpers: true
2525

2626
# Import common connection and controller functions to use in pipelines
2727
import Plug.Conn

0 commit comments

Comments
 (0)