2022-10-25 11:07:07 +13:00
|
|
|
defmodule Example.AuthPlug do
|
|
|
|
@moduledoc false
|
|
|
|
use AshAuthentication.Plug, otp_app: :ash_authentication
|
|
|
|
|
|
|
|
@impl true
|
2022-11-04 11:24:33 +13:00
|
|
|
|
2022-11-23 09:09:41 +13:00
|
|
|
def handle_success(conn, {strategy, phase}, nil, nil) do
|
2022-11-04 11:24:33 +13:00
|
|
|
conn
|
|
|
|
|> put_resp_header("content-type", "application/json")
|
|
|
|
|> send_resp(
|
|
|
|
200,
|
2022-11-23 09:09:41 +13:00
|
|
|
Jason.encode!(%{status: :success, strategy: strategy, phase: phase})
|
2022-11-04 11:24:33 +13:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-11-23 09:09:41 +13:00
|
|
|
def handle_success(conn, {strategy, phase}, user, token) do
|
2022-10-25 11:07:07 +13:00
|
|
|
conn
|
2022-10-31 16:43:00 +13:00
|
|
|
|> store_in_session(user)
|
|
|
|
|> put_resp_header("content-type", "application/json")
|
|
|
|
|> send_resp(
|
|
|
|
200,
|
|
|
|
Jason.encode!(%{
|
2022-11-04 11:24:33 +13:00
|
|
|
status: :success,
|
2022-10-31 16:43:00 +13:00
|
|
|
token: token,
|
2023-01-11 15:12:53 +13:00
|
|
|
user: Map.take(user, ~w[username id email]a),
|
2022-11-23 09:09:41 +13:00
|
|
|
strategy: strategy,
|
|
|
|
phase: phase
|
2022-10-31 16:43:00 +13:00
|
|
|
})
|
|
|
|
)
|
2022-10-25 11:07:07 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2022-11-23 09:09:41 +13:00
|
|
|
def handle_failure(conn, {strategy, phase}, reason) do
|
2022-10-25 11:07:07 +13:00
|
|
|
conn
|
2022-10-31 16:43:00 +13:00
|
|
|
|> put_resp_header("content-type", "application/json")
|
|
|
|
|> send_resp(
|
|
|
|
401,
|
|
|
|
Jason.encode!(%{
|
2022-11-04 11:24:33 +13:00
|
|
|
status: :failure,
|
2022-11-23 09:09:41 +13:00
|
|
|
reason: inspect(reason),
|
|
|
|
strategy: strategy,
|
|
|
|
phase: phase
|
2022-10-31 16:43:00 +13:00
|
|
|
})
|
|
|
|
)
|
2022-10-25 11:07:07 +13:00
|
|
|
end
|
|
|
|
end
|