Zoonk.Accounts (Zoonk v0.1.0-dev)

View Source

Manages 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.

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's settings.

Functions

change_user_email(user, attrs \\ %{}, opts \\ [])

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{}}

change_user_profile(user_profile, attrs \\ %{})

Returns an %Ecto.Changeset{} for tracking a user's profile changes.

Examples

iex> change_user_profile(%UserProfile{}, %{field: new_value})
%Ecto.Changeset{data: %UserProfile{}}

change_user_settings(user, attrs \\ %{})

Returns an %Ecto.Changeset{} for tracking a user's settings changes.

Examples

iex> change_user_settings(%User{}, %{language: :en})
%Ecto.Changeset{data: %User{}}

delete_user_session_token(token)

Deletes the signed token with the given context.

deliver_login_instructions(user)

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}

deliver_user_update_email_instructions(user, current_email)

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}

generate_user_session_token(user, opts \\ [decoded: true])

Generates a session token.

get_user_by_email(email)

Gets a user by email.

Examples

iex> get_user_by_email("foo@example.com")
%User{}

iex> get_user_by_email("unknown@example.com")
nil

get_user_by_session_token(token)

Gets the user with the given signed token.

If the token is valid {user, token_inserted_at} is returned, otherwise nil is returned.

list_providers()

Returns a list of supported oAuth providers.

Example

iex> list_providers()
[:apple, :github, :google]

login_user_by_otp(otp_code, email)

Logs the user in by OTP code.

There are three cases to consider:

  1. The user has already confirmed their email. They are logged in and the OTP code is expired.

  2. 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.

login_with_provider(auth, scope, language)

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{}}

signup_user(attrs, scope)

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}

sudo_mode?(user)

Checks whether the user is in sudo mode.

The user is in sudo mode when the last authentication was done recently.

update_user_email(user, otp_code)

Updates the user email using the given OTP code.

If the code matches, the user email is updated and the code is deleted.

update_user_profile(scope, profile, attrs)

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{}}

update_user_settings(scope, attrs)

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{}}