Skip to content

Commit

Permalink
chore: Add spark DSL cheatsheets to docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsynz committed Sep 21, 2023
1 parent 4438fa7 commit 8667618
Show file tree
Hide file tree
Showing 12 changed files with 2,553 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .check.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
## custom new tools may be added (Mix tasks or arbitrary commands)
# {:my_task, "mix my_task", env: %{"MIX_ENV" => "prod"}},
# {:my_tool, ["my_tool", "arg with spaces"]}
{:credo, "mix credo --strict"}
{:credo, "mix credo --strict"},
{:spark_formatter, "mix spark.formatter --check"},
{:spark_cheat_sheets, "mix spark.cheat_sheets --check"}
]
]
187 changes: 187 additions & 0 deletions documentation/dsls/DSL:-AshAuthentication.AddOn.Confirmation.cheatmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# DSL: AshAuthentication.AddOn.Confirmation

Confirmation support.

Sometimes when creating a new user, or changing a sensitive attribute (such as
their email address) you may want to wait for the user to confirm by way of
sending them a confirmation token to prove that it was really them that took
the action.

In order to add confirmation to your resource, it must been the following
minimum requirements:

1. Have a primary key
2. Have at least one attribute you wish to confirm
3. Tokens must be enabled

## Example

```elixir
defmodule MyApp.Accounts.User do
use Ash.Resource,
extensions: [AshAuthentication]

attributes do
uuid_primary_key :id
attribute :email, :ci_string, allow_nil?: false
end

authentication do
api MyApp.Accounts

add_ons do
confirmation :confirm do
monitor_fields [:email]
sender MyApp.ConfirmationSender
end
end

strategies do
# ...
end
end

identities do
identity :email, [:email] do
eager_check_with MyApp.Accounts
end
end
end
```

## Attributes

A `confirmed_at` attribute will be added to your resource if it's not already
present (see `confirmed_at_field` in the DSL documentation).

## Actions

By default confirmation will add an action which updates the `confirmed_at`
attribute as well as retrieving previously stored changes and applying them to
the resource.

If you wish to perform the confirm action directly from your code you can do
so via the `AshAuthentication.Strategy` protocol.

### Example

iex> strategy = Info.strategy!(Example.User, :confirm)
...> {:ok, user} = Strategy.action(strategy, :confirm, %{"confirm" => confirmation_token()})
...> user.confirmed_at >= one_second_ago()
true

## Plugs

Confirmation provides a single endpoint for the `:confirm` phase. If you wish
to interact with the plugs directly, you can do so via the
`AshAuthentication.Strategy` protocol.

### Example

iex> strategy = Info.strategy!(Example.User, :confirm)
...> conn = conn(:get, "/user/confirm", %{"confirm" => confirmation_token()})
...> conn = Strategy.plug(strategy, :confirm, conn)
...> {_conn, {:ok, user}} = Plug.Helpers.get_authentication_result(conn)
...> user.confirmed_at >= one_second_ago()
true

## DSL Documentation

User confirmation flow





* `:name` (`t:atom/0`) - Required. Uniquely identifies the add-on.

* `:token_lifetime` (`t:pos_integer/0`) - How long should the confirmation token be valid, in hours.
Defaults to 3 days. The default value is `72`.

* `:monitor_fields` (list of `t:atom/0`) - Required. A list of fields to monitor for changes (eg `[:email, :phone_number]`).
The confirmation will only be sent when one of these fields are changed.

* `:confirmed_at_field` (`t:atom/0`) - The name of a field to store the time that the last confirmation took
place.
This attribute will be dynamically added to the resource if not already
present. The default value is `:confirmed_at`.

* `:confirm_on_create?` (`t:boolean/0`) - Generate and send a confirmation token when a new resource is created?
Will only trigger when a create action is executed _and_ one of the
monitored fields is being set. The default value is `true`.

* `:confirm_on_update?` (`t:boolean/0`) - Generate and send a confirmation token when a resource is changed?
Will only trigger when an update action is executed _and_ one of the
monitored fields is being set. The default value is `true`.

* `:inhibit_updates?` (`t:boolean/0`) - Wait until confirmation is received before actually changing a monitored
field?
If a change to a monitored field is detected, then the change is stored
in the token resource and the changeset updated to not make the
requested change. When the token is confirmed, the change will be
applied.
This could be potentially weird for your users, but useful in the case
of a user changing their email address or phone number where you want
to verify that the new contact details are reachable. The default value is `true`.

* `:sender` - Required. How to send the confirmation instructions to the user.
Allows you to glue sending of confirmation instructions to
[swoosh](https://hex.pm/packages/swoosh),
[ex_twilio](https://hex.pm/packages/ex_twilio) or whatever notification
system is appropriate for your application.
Accepts a module, module and opts, or a function that takes a record,
reset token and options.
The options will be a keyword list containing the original
changeset, before any changes were inhibited. This allows you
to send an email to the user's new email address if it is being
changed for example.
See `AshAuthentication.Sender` for more information.

* `:confirm_action_name` (`t:atom/0`) - The name of the action to use when performing confirmation.
If this action is not already present on the resource, it will be
created for you. The default value is `:confirm`.








## authentication.add_ons.confirmation
```elixir
confirmation name \ :confirm
```


User confirmation flow





### Arguments
| Name | Type | Default | Docs |
| --- | --- | --- | --- |
| `name`* | `atom` | | Uniquely identifies the add-on. |
### Options
| Name | Type | Default | Docs |
| --- | --- | --- | --- |
| `monitor_fields`* | `list(atom)` | | A list of fields to monitor for changes (eg `[:email, :phone_number]`). The confirmation will only be sent when one of these fields are changed. |
| `sender`* | `(any, any, any -> any) \| module` | | How to send the confirmation instructions to the user. Allows you to glue sending of confirmation instructions to [swoosh](https://hex.pm/packages/swoosh), [ex_twilio](https://hex.pm/packages/ex_twilio) or whatever notification system is appropriate for your application. Accepts a module, module and opts, or a function that takes a record, reset token and options. The options will be a keyword list containing the original changeset, before any changes were inhibited. This allows you to send an email to the user's new email address if it is being changed for example. See `AshAuthentication.Sender` for more information. |
| `token_lifetime` | `pos_integer` | `72` | How long should the confirmation token be valid, in hours. Defaults to 3 days. |
| `confirmed_at_field` | `atom` | `:confirmed_at` | The name of a field to store the time that the last confirmation took place. This attribute will be dynamically added to the resource if not already present. |
| `confirm_on_create?` | `boolean` | `true` | Generate and send a confirmation token when a new resource is created? Will only trigger when a create action is executed _and_ one of the monitored fields is being set. |
| `confirm_on_update?` | `boolean` | `true` | Generate and send a confirmation token when a resource is changed? Will only trigger when an update action is executed _and_ one of the monitored fields is being set. |
| `inhibit_updates?` | `boolean` | `true` | Wait until confirmation is received before actually changing a monitored field? If a change to a monitored field is detected, then the change is stored in the token resource and the changeset updated to not make the requested change. When the token is confirmed, the change will be applied. This could be potentially weird for your users, but useful in the case of a user changing their email address or phone number where you want to verify that the new contact details are reachable. |
| `confirm_action_name` | `atom` | `:confirm` | The name of the action to use when performing confirmation. If this action is not already present on the resource, it will be created for you. |





### Introspection

Target: `AshAuthentication.AddOn.Confirmation`


Loading

0 comments on commit 8667618

Please sign in to comment.