You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: elixir/phoenix/AUTHENTICATION.md
+9-10Lines changed: 9 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Phoenix ships with an [Authentication code generator](https://hexdocs.pm/phoenix/mix_phx_gen_auth.html), which will be the preferred to use in any project that requires authentication unless the user or the project README says otherwise.
4
4
5
-
**IMPORTANT:** Before adding Authentication as instructed here, an Intent needs to be created with instruction from points 2. and 3. of this document, with user approval. The Intent **MUST** be created as specified by the INTENT_SPECIFICATION.md and exemplified by the INTENT_EXAMPLE.md.
5
+
**IMPORTANT:** Before adding Authentication as instructed here, an Intent needs to be created with instructions from points 2. and 3. of this document, with user approval. The Intent **MUST** be created as specified by the INTENT_SPECIFICATION.md and exemplified by the INTENT_EXAMPLE.md.
6
6
7
7
## 1. Overview
8
8
@@ -11,19 +11,18 @@ The generator by default:
11
11
* uses magic links, not email and password combo, but it can be enabled by the user after email confirmation.
12
12
* adds the routes to `router.ex`.
13
13
* adds the tests.
14
-
* adds a [Scope](https://hexdocs.pm/phoenix/mix_phx_gen_auth.html#scopes) module since Phoenix 1.8. The `%Scope{}` is then passed calls to the business logic layer (`lib/my_app/*`) to prevent unauthorized access to other users resources.
14
+
* adds a [Scope](https://hexdocs.pm/phoenix/mix_phx_gen_auth.html#scopes) module since Phoenix 1.8. The `%Scope{}` is then passed in calls to the business logic layer (`lib/my_app/*`) to prevent unauthorized access to other users resources.
15
15
* doesn't include a production mailer.
16
16
17
17
## 2. How to Install
18
18
19
19
1. Run `mix phx.gen.auth Accounts.Users User users --live --hashing-lib argon2 --web Accounts.Users`.
20
20
2. Run `mix deps.get`.
21
-
4. Run `mix ecto.migrate`.
22
-
5. Run `mix ecto.migrate`.
21
+
3. Run `mix ecto.migrate`.
23
22
24
23
## 3. How to Add Routes that Require Authentication
25
24
26
-
For example, when you create a LiveView with `mix phx.gen.live Catalogs.Products Product products name:string desc:string --web Catalogs.Products` then it will ask you to add this routes:
25
+
For example, when you create a LiveView with `mix phx.gen.live Catalogs.Products Product products name:string desc:string --web Catalogs.Products` then it will ask you to add these routes:
@@ -53,15 +52,15 @@ scope "/catalogs/products", MyAppWeb.Catalogs.Products do
53
52
end
54
53
```
55
54
56
-
**CRITICAL:** For security reasons you **MUST**use always the `on_mount` with `:require_authenticated`. You **MUST NOT** use the `on_mount` with `:mount_current_scope` unless explicitly asked by the user. The use of `:mount_current_scope` will not protect the route at the router level when an attacker replays an authenticated request, instead it will crash somewhere else on the code base where the current scope needs to be used and it's `nil`, which can be at business layer level, before doing a database request. It's enough for the developer or AI agent to not use a scope in the query to make the attacker happy.
55
+
**CRITICAL:** For security reasons you **MUST** always use the `on_mount` with `:require_authenticated`. You **MUST NOT** use the `on_mount` with `:mount_current_scope` unless explicitly asked by the user. The use of `:mount_current_scope` will not protect the route at the router level when an attacker replays an authenticated request, instead it will crash somewhere else in the code base where the current scope needs to be used and it's `nil`, which can be at the business layer level, before doing a database request. It's enough for the developer or AI agent to not use a scope in the query to make the attacker happy.
57
56
58
-
The live session was named `:catalogs_products_require_authenticated_user`, as per the above pattern, which guarantees that it will be always unique in the router.
57
+
The live session was named `:catalogs_products_require_authenticated_user`, as per the above pattern, which guarantees that it will always be unique in the router.
59
58
60
-
Notice, that each live route had `products` removed from it, which its required when we use the option `--web` with the format `-web <DomainPlural>.<ResourcePlural>`, e.g. `--web Catalogs.Products`. This is because of a bug in the code generators that will create routes with the resource duplicated in the path, that in this case was `/catalogs/products/products/...`.
59
+
Notice, that each live route had `products` removed from it, which is required when we use the option `--web` with the format `-web <DomainPlural>.<ResourcePlural>`, e.g. `--web Catalogs.Products`. This is because of a bug in the code generators that will create routes with the resource duplicated in the path, that in this case was `/catalogs/products/products/...`.
61
60
62
-
Now its also required to fix all routes created by the code generator in other files, and to fix them:
61
+
Now it's also required to fix all routes created by the code generator in other files, and to fix them:
63
62
1. You **MUST** run `mix compile` to find all module files with routes to be fixed.
64
-
2. You **MUST** then create a list of files from the the output of `mix compile` that have routes to be fixed.
63
+
2. You **MUST** then create a list of files from the output of `mix compile` that have routes to be fixed.
65
64
3. You **MUST** feed the list of files to a **find and replace tool** (e.g. `find` in Linux), to fix all routes in only one pass. You **MUST NOT** try to fix one module at a time, because that will be an exhaustive and time consuming task.
0 commit comments