2022-10-25 11:23:05 +13:00
|
|
|
defmodule Example.Accounts.User do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: Ash.DataLayer.Ets,
|
2022-12-08 14:32:26 +13:00
|
|
|
extensions: [AshAuthentication]
|
2022-11-03 16:07:25 +13:00
|
|
|
|
|
|
|
require Logger
|
2022-10-25 11:23:05 +13:00
|
|
|
|
2022-10-25 11:27:36 +13:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
id: Ecto.UUID.t(),
|
|
|
|
email: String.t(),
|
|
|
|
hashed_password: String.t(),
|
|
|
|
created_at: DateTime.t(),
|
|
|
|
updated_at: DateTime.t()
|
|
|
|
}
|
|
|
|
|
2022-10-25 11:23:05 +13:00
|
|
|
actions do
|
|
|
|
defaults([:read])
|
2022-11-15 11:52:13 +13:00
|
|
|
|
|
|
|
create :register_with_auth0 do
|
|
|
|
argument :user_info, :map, allow_nil?: false
|
|
|
|
argument :oauth_tokens, :map, allow_nil?: false
|
|
|
|
upsert? true
|
|
|
|
upsert_identity :unique_email
|
|
|
|
|
|
|
|
change AshAuthentication.GenerateTokenChange
|
|
|
|
|
|
|
|
change fn changeset, _ ->
|
|
|
|
user_info = Ash.Changeset.get_argument(changeset, :user_info)
|
|
|
|
|
|
|
|
changeset
|
|
|
|
|> Ash.Changeset.change_attribute(:email, user_info["email"])
|
|
|
|
end
|
|
|
|
end
|
2023-01-12 19:16:23 +13:00
|
|
|
|
|
|
|
create :register_with_github do
|
|
|
|
argument :user_info, :map, allow_nil?: false
|
|
|
|
argument :oauth_tokens, :map, allow_nil?: false
|
|
|
|
upsert? true
|
|
|
|
upsert_identity :unique_email
|
|
|
|
|
|
|
|
change AshAuthentication.GenerateTokenChange
|
|
|
|
|
|
|
|
change fn changeset, _ ->
|
|
|
|
user_info = Ash.Changeset.get_argument(changeset, :user_info)
|
|
|
|
|
|
|
|
changeset
|
|
|
|
|> Ash.Changeset.change_attribute(:email, user_info["email"])
|
|
|
|
end
|
|
|
|
end
|
2022-10-25 11:23:05 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
|
|
|
|
|
|
|
attribute(:email, :ci_string, allow_nil?: false)
|
2022-11-15 11:52:13 +13:00
|
|
|
attribute(:hashed_password, :string, allow_nil?: true, sensitive?: true, private?: true)
|
2022-10-25 11:23:05 +13:00
|
|
|
|
|
|
|
create_timestamp(:created_at)
|
|
|
|
update_timestamp(:updated_at)
|
|
|
|
end
|
|
|
|
|
2022-12-08 14:32:26 +13:00
|
|
|
# wat 2
|
2022-10-25 11:23:05 +13:00
|
|
|
authentication do
|
|
|
|
api(Example.Accounts)
|
|
|
|
|
2022-12-08 14:32:26 +13:00
|
|
|
add_ons do
|
|
|
|
confirmation :confirm do
|
|
|
|
monitor_fields([:email])
|
2022-11-05 18:14:37 +13:00
|
|
|
|
2022-12-08 14:32:26 +13:00
|
|
|
sender(fn user, token ->
|
|
|
|
Logger.debug("Confirmation request for #{user.email} with token #{inspect(token)}")
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2022-11-05 18:14:37 +13:00
|
|
|
|
2022-12-08 14:32:26 +13:00
|
|
|
strategies do
|
2023-01-12 19:16:23 +13:00
|
|
|
password do
|
2022-12-08 14:32:26 +13:00
|
|
|
identity_field(:email)
|
|
|
|
hashed_password_field(:hashed_password)
|
2022-10-25 11:23:05 +13:00
|
|
|
|
2022-12-08 14:32:26 +13:00
|
|
|
resettable do
|
|
|
|
sender(fn user, token ->
|
|
|
|
Logger.debug("Password reset request for #{user.email} with token #{inspect(token)}")
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
2022-11-03 16:07:25 +13:00
|
|
|
|
2023-01-12 19:16:23 +13:00
|
|
|
auth0 do
|
2022-12-08 14:32:26 +13:00
|
|
|
client_id(&get_config/2)
|
|
|
|
redirect_uri(&get_config/2)
|
|
|
|
client_secret(&get_config/2)
|
|
|
|
site(&get_config/2)
|
|
|
|
end
|
2023-01-12 19:16:23 +13:00
|
|
|
|
|
|
|
github do
|
|
|
|
client_id &get_config/2
|
|
|
|
redirect_uri &get_config/2
|
|
|
|
client_secret &get_config/2
|
|
|
|
end
|
2022-12-08 14:32:26 +13:00
|
|
|
end
|
2022-11-22 14:34:48 +13:00
|
|
|
|
2022-12-08 14:32:26 +13:00
|
|
|
tokens do
|
|
|
|
enabled?(true)
|
|
|
|
token_resource(Example.Accounts.Token)
|
2022-12-15 16:31:52 +13:00
|
|
|
|
|
|
|
signing_secret(fn _, _ ->
|
|
|
|
Application.fetch_env(:ash_authentication_phoenix, :signing_secret)
|
|
|
|
end)
|
2022-12-08 14:32:26 +13:00
|
|
|
end
|
2022-11-03 16:07:25 +13:00
|
|
|
end
|
|
|
|
|
2022-10-25 11:23:05 +13:00
|
|
|
identities do
|
2022-12-08 14:32:26 +13:00
|
|
|
identity(:unique_email, [:email],
|
|
|
|
pre_check_with: Example.Accounts,
|
|
|
|
eager_check_with: Example.Accounts
|
|
|
|
)
|
2022-10-25 11:23:05 +13:00
|
|
|
end
|
2022-11-22 14:34:48 +13:00
|
|
|
|
2022-12-08 14:32:26 +13:00
|
|
|
def get_config(path, resource) do
|
2022-11-22 14:34:48 +13:00
|
|
|
value =
|
|
|
|
:ash_authentication_phoenix
|
|
|
|
|> Application.get_env(resource, [])
|
|
|
|
|> get_in(path)
|
|
|
|
|
|
|
|
{:ok, value}
|
|
|
|
end
|
2022-10-25 11:23:05 +13:00
|
|
|
end
|