mirror of
https://github.com/team-alembic/ash_authentication.git
synced 2024-09-20 05:13:10 +12:00
d5c5d6b6c5
* Adds the `require_token_presence_for_authentication?` DSL option to the Authentication extension which when enabled changes the following behaviour: 1. The `store_in_session` plug will store the user's token rather than their subject in the session. 2. The `retrieve_from_session` plug will look for a stored token in the session rather than a subject and ensure that it's present in the `TokenResource`. 3. The `retrieve_from_bearer` plug will ensure that the token is present in the `TokenResource`. * Adds the `get_token` action to the `TokenResource`.
60 lines
1.3 KiB
Elixir
60 lines
1.3 KiB
Elixir
defmodule Example.UserWithTokenRequired do
|
|
@moduledoc false
|
|
use Ash.Resource, data_layer: AshPostgres.DataLayer, extensions: [AshAuthentication]
|
|
|
|
@type t :: %__MODULE__{
|
|
id: Ecto.UUID.t(),
|
|
email: String.t(),
|
|
hashed_password: String.t(),
|
|
created_at: DateTime.t(),
|
|
updated_at: DateTime.t()
|
|
}
|
|
|
|
attributes do
|
|
uuid_primary_key :id, writable?: true
|
|
attribute :email, :ci_string, allow_nil?: false
|
|
attribute :hashed_password, :string, allow_nil?: true, sensitive?: true, private?: true
|
|
create_timestamp :created_at
|
|
update_timestamp :updated_at
|
|
end
|
|
|
|
authentication do
|
|
api Example
|
|
|
|
tokens do
|
|
enabled? true
|
|
store_all_tokens? true
|
|
require_token_presence_for_authentication? true
|
|
token_resource Example.Token
|
|
signing_secret &get_config/2
|
|
end
|
|
|
|
strategies do
|
|
password do
|
|
identity_field :email
|
|
end
|
|
end
|
|
end
|
|
|
|
actions do
|
|
defaults [:create, :read, :update, :destroy]
|
|
end
|
|
|
|
identities do
|
|
identity :email, [:email], eager_check_with: Example
|
|
end
|
|
|
|
postgres do
|
|
table "user_with_token_required"
|
|
repo(Example.Repo)
|
|
end
|
|
|
|
def get_config(path, _resource) do
|
|
value =
|
|
:ash_authentication
|
|
|> Application.get_all_env()
|
|
|> get_in(path)
|
|
|
|
{:ok, value}
|
|
end
|
|
end
|