mirror of
https://github.com/team-alembic/ash_authentication.git
synced 2024-09-20 13:24:20 +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`.
46 lines
1 KiB
Elixir
46 lines
1 KiB
Elixir
defmodule Example.AuthPlug do
|
|
@moduledoc false
|
|
use AshAuthentication.Plug, otp_app: :ash_authentication
|
|
|
|
@impl true
|
|
|
|
def handle_success(conn, {strategy, phase}, nil, nil) do
|
|
conn
|
|
|> put_resp_header("content-type", "application/json")
|
|
|> send_resp(
|
|
200,
|
|
Jason.encode!(%{status: :success, strategy: strategy, phase: phase})
|
|
)
|
|
end
|
|
|
|
def handle_success(conn, {strategy, phase}, user, token) do
|
|
conn
|
|
|> store_in_session(user)
|
|
|> put_resp_header("content-type", "application/json")
|
|
|> send_resp(
|
|
200,
|
|
Jason.encode!(%{
|
|
status: :success,
|
|
token: token,
|
|
user: Map.take(user, ~w[username id email]a),
|
|
strategy: strategy,
|
|
phase: phase
|
|
})
|
|
)
|
|
end
|
|
|
|
@impl true
|
|
def handle_failure(conn, {strategy, phase}, reason) do
|
|
conn
|
|
|> put_resp_header("content-type", "application/json")
|
|
|> send_resp(
|
|
401,
|
|
Jason.encode!(%{
|
|
status: :failure,
|
|
reason: inspect(reason),
|
|
strategy: strategy,
|
|
phase: phase
|
|
})
|
|
)
|
|
end
|
|
end
|