defmodule Iterup.Account.UserToken do
use Ash.Resource,
data_layer: AshPostgres.DataLayer
@rand_size 32
postgres do
table("user_tokens")
repo(Iterup.Repo)
end
attributes do
uuid_primary_key(:id)
attribute :sent_to, :ci_string, allow_nil?: false
attribute :token, :binary, allow_nil?: false
attribute :context, :string, allow_nil?: false
attribute :confirmed_at, :utc_datetime
create_timestamp :inserted_at
end
relationships do
belongs_to :user, Iterup.Account.User
end
identities do
identity :context_token, [:context, :token]
end
code_interface do
define_for(Iterup.Account)
define(:create_session_token, action: :session_token)
end
actions do
create :session_token do
accept [:sent_to]
argument :user_id, :uuid, allow_nil?: false
change manage_relationship(:user_id, :user, type: :append)
change set_attribute(:token, :crypto.strong_rand_bytes(@rand_size))
change set_attribute(:context, "session")
end
end
end
defmodule Iterup.Account.UserToken do
use Ash.Resource,
data_layer: AshPostgres.DataLayer
@rand_size 32
postgres do
table("user_tokens")
repo(Iterup.Repo)
end
attributes do
uuid_primary_key(:id)
attribute :sent_to, :ci_string, allow_nil?: false
attribute :token, :binary, allow_nil?: false
attribute :context, :string, allow_nil?: false
attribute :confirmed_at, :utc_datetime
create_timestamp :inserted_at
end
relationships do
belongs_to :user, Iterup.Account.User
end
identities do
identity :context_token, [:context, :token]
end
code_interface do
define_for(Iterup.Account)
define(:create_session_token, action: :session_token)
end
actions do
create :session_token do
accept [:sent_to]
argument :user_id, :uuid, allow_nil?: false
change manage_relationship(:user_id, :user, type: :append)
change set_attribute(:token, :crypto.strong_rand_bytes(@rand_size))
change set_attribute(:context, "session")
end
end
end