Zoonk.Accounts (Zoonk v0.1.0-dev)
View SourceManages user accounts.
This module handles core account management flows including user signup, session management, and email verification.
It coordinates with the database layer to manage user records and tokens, while enforcing security measures like sudo mode and token expiration.
Summary
Functions
Returns an %Ecto.Changeset{}
for changing the user email.
Returns an %Ecto.Changeset{}
for tracking a user's profile changes.
Returns an %Ecto.Changeset{}
for tracking a user's settings changes.
Deletes the signed token with the given context.
Delivers the OTP code login instructions to the given user.
Delivers the update email instructions to the given user.
Generates a session token.
Gets a user by email.
Gets the user with the given signed token.
Returns a list of supported oAuth providers.
Logs the user in by OTP code.
Signs in a user with a third-party provider.
Signs up a user.
Checks whether the user is in sudo mode.
Updates the user email using the given OTP code.
Updates a user profile.
Updates a user's settings.
Functions
Returns an %Ecto.Changeset{}
for changing the user email.
See Zoonk.Accounts.User.email_changeset/3
for a list of supported options.
Examples
iex> change_user_email(user)
%Ecto.Changeset{data: %User{}}
Returns an %Ecto.Changeset{}
for tracking a user's profile changes.
Examples
iex> change_user_profile(%UserProfile{}, %{field: new_value})
%Ecto.Changeset{data: %UserProfile{}}
Returns an %Ecto.Changeset{}
for tracking a user's settings changes.
Examples
iex> change_user_settings(%User{}, %{language: :en})
%Ecto.Changeset{data: %User{}}
Deletes the signed token with the given context.
Delivers the OTP code login instructions to the given user.
Examples
iex> deliver_login_instructions(user)
{:ok, %{to: ..., body: ...}}
iex> deliver_login_instructions(user)
{:error, :rate_limit_exceeded}
Delivers the update email instructions to the given user.
Examples
iex> deliver_user_update_email_instructions(user, current_email)
{:ok, %{to: ..., body: ...}}
iex> deliver_user_update_email_instructions(user, current_email)
{:error, :rate_limit_exceeded}
Generates a session token.
Gets a user by email.
Examples
iex> get_user_by_email("foo@example.com")
%User{}
iex> get_user_by_email("unknown@example.com")
nil
Gets the user with the given signed token.
If the token is valid {user, token_inserted_at}
is returned,
otherwise nil
is returned.
Returns a list of supported oAuth providers.
Example
iex> list_providers()
[:apple, :github, :google]
Logs the user in by OTP code.
There are three cases to consider:
The user has already confirmed their email. They are logged in and the OTP code is expired.
The user has not confirmed their email. In this case, the user gets confirmed, logged in, and all tokens - including session ones - are expired. In theory, no other tokens exist but we delete all of them for best security practices.
Signs in a user with a third-party provider.
It either links the provider to an existing user or signs up a new user and links the provider.
Examples
iex> login_with_provider(%{}, %Scope{}, "en")
{:ok, %User{}}
iex> login_with_provider(nil, %Scope{}, "en")
{:error, %Ecto.Changeset{}}
Signs up a user.
Examples
iex> signup_user(%{field: value}, %Scope{})
{:ok, %User{}}
iex> signup_user(%{field: bad_value}, %Scope{})
{:error, %Ecto.Changeset{}}
iex> signup_user(%{field: value}, nil)
{:error, :not_allowed}
Checks whether the user is in sudo mode.
The user is in sudo mode when the last authentication was done recently.
Updates the user email using the given OTP code.
If the code matches, the user email is updated and the code is deleted.
Updates a user profile.
Examples
iex> update_user_profile(%Scope{}, %UserProfile{}, %{display_name: "New Name"})
{:ok, %UserProfile{}}
iex> update_user_profile(%Scope{}, %UserProfile{}, %{display_name: bad_value})
{:error, %Ecto.Changeset{}}
Updates a user's settings.
Examples
iex> update_user_settings(%Scope{}, %{language: :en})
{:ok, %User{}}
iex> update_user_settings(%Scope{}, %{language: :invalid})
{:error, %Ecto.Changeset{}}