View Source Zoonk.Accounts (Zoonk v0.3.0-alpha)

Accounts context.

This context manages user accounts, including registration, login, and logout. It's not responsible for school users, which are managed by the Zoonk.Organizations context.

Link to this section Summary

Functions

Emulates that the email will change without actually changing it in the database.

Returns an %Ecto.Changeset{} for changing the user email.

Returns an %Ecto.Changeset{} for changing the user password.

Returns an %Ecto.Changeset{} for tracking user changes.

Returns an %Ecto.Changeset{} for changing the user settings.

Confirms a user by the given token.

Deletes the signed token with the given context.

Delivers the confirmation email instructions to the given user.

Delivers the reset password email to the given user.

Delivers the update email instructions to the given user.

Generates a session token.

Gets a single user.

Gets a user by email.

Gets a user by either their email address or username.

Gets the user by reset password token.

Gets the user with the given signed token.

Gets a user by username.

Registers a user.

Resets the user password.

Updates the user email using the given token.

Updates the user settings.

Link to this section Types

@type user_changeset() ::
  {:ok, Zoonk.Accounts.User.t()} | {:error, Ecto.Changeset.t()}

Link to this section Functions

Link to this function

apply_user_email(user, password, attrs)

View Source
@spec apply_user_email(Zoonk.Accounts.User.t(), String.t(), map()) :: user_changeset()

Emulates that the email will change without actually changing it in the database.

examples

Examples

iex> apply_user_email(user, "valid password", %{email: ...})
{:ok, %User{}}

iex> apply_user_email(user, "invalid password", %{email: ...})
{:error, %Ecto.Changeset{}}
Link to this function

change_user_email(user, attrs \\ %{})

View Source
@spec change_user_email(Zoonk.Accounts.User.t(), map()) :: Ecto.Changeset.t()

Returns an %Ecto.Changeset{} for changing the user email.

examples

Examples

iex> change_user_email(user)
%Ecto.Changeset{data: %User{}}
Link to this function

change_user_password(user, attrs \\ %{})

View Source
@spec change_user_password(Zoonk.Accounts.User.t(), map()) :: Ecto.Changeset.t()

Returns an %Ecto.Changeset{} for changing the user password.

examples

Examples

iex> change_user_password(user)
%Ecto.Changeset{data: %User{}}
Link to this function

change_user_registration(user, attrs \\ %{})

View Source
@spec change_user_registration(Zoonk.Accounts.User.t(), map()) :: Ecto.Changeset.t()

Returns an %Ecto.Changeset{} for tracking user changes.

examples

Examples

iex> change_user_registration(user)
%Ecto.Changeset{data: %User{}}
Link to this function

change_user_settings(user, attrs \\ %{})

View Source
@spec change_user_settings(Zoonk.Accounts.User.t(), map()) :: Ecto.Changeset.t()

Returns an %Ecto.Changeset{} for changing the user settings.

examples

Examples

iex> change_user_settings(user)
%Ecto.Changeset{data: %User{}}
@spec confirm_user(binary()) :: {:ok, Zoonk.Accounts.User.t()} | :error

Confirms a user by the given token.

If the token matches, the user account is marked as confirmed and the token is deleted.

Link to this function

delete_user_session_token(token)

View Source
@spec delete_user_session_token(binary()) :: :ok

Deletes the signed token with the given context.

Link to this function

deliver_user_confirmation_instructions(user, confirmation_url_fun)

View Source
@spec deliver_user_confirmation_instructions(
  Zoonk.Accounts.User.t(),
  (String.t() -> String.t())
) ::
  Zoonk.Accounts.UserNotifier.deliver() | {:error, :already_confirmed}

Delivers the confirmation email instructions to the given user.

examples

Examples

iex> deliver_user_confirmation_instructions(user, &url(~p"/users/confirm/#{&1}"))
{:ok, %{to: ..., body: ...}}

iex> deliver_user_confirmation_instructions(confirmed_user, &url(~p"/users/confirm/#{&1}"))
{:error, :already_confirmed}
Link to this function

deliver_user_reset_password_instructions(user, reset_password_url_fun)

View Source
@spec deliver_user_reset_password_instructions(
  Zoonk.Accounts.User.t(),
  (String.t() -> String.t())
) ::
  Zoonk.Accounts.UserNotifier.deliver()

Delivers the reset password email to the given user.

examples

Examples

iex> deliver_user_reset_password_instructions(user, &url(~p"/users/reset_password/#{&1}"))
{:ok, %{to: ..., body: ...}}
Link to this function

deliver_user_update_email_instructions(user, current_email, update_email_url_fun)

View Source
@spec deliver_user_update_email_instructions(
  Zoonk.Accounts.User.t(),
  String.t(),
  (String.t() -> String.t())
) :: Zoonk.Accounts.UserNotifier.deliver()

Delivers the update email instructions to the given user.

examples

Examples

iex> deliver_user_update_email_instructions(user, current_email, &url(~p"/users/settings/confirm_email/#{&1})")
{:ok, %{to: ..., body: ...}}
Link to this function

generate_user_session_token(user)

View Source
@spec generate_user_session_token(Zoonk.Accounts.User.t()) :: binary()

Generates a session token.

@spec get_user!(integer()) :: Zoonk.Accounts.User.t()

Gets a single user.

Raises Ecto.NoResultsError if the User does not exist.

examples

Examples

iex> get_user!(123)
%User{}

iex> get_user!(456)
** (Ecto.NoResultsError)
Link to this function

get_user_by_email(email)

View Source
@spec get_user_by_email(String.t()) :: Zoonk.Accounts.User.t() | nil

Gets a user by email.

examples

Examples

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

iex> get_user_by_email("unknown@example.com")
nil
Link to this function

get_user_by_email_or_username(email_or_username)

View Source
@spec get_user_by_email_or_username(String.t()) :: Zoonk.Accounts.User.t() | nil

Gets a user by either their email address or username.

examples

Examples

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

iex> get_user_by_username("davinci")
%User{}
Link to this function

get_user_by_email_or_username_and_password(email_or_username, password)

View Source
@spec get_user_by_email_or_username_and_password(String.t(), String.t()) ::
  Zoonk.Accounts.User.t() | nil

Gets a user by email and password.

examples

Examples

iex> get_user_by_email_or_username_and_password("foo@example.com", "correct_password")
%User{}

iex> get_user_by_email_or_username_and_password("adalovelace", "correct_password")
%User{}

iex> get_user_by_email_or_username_and_password("foo@example.com", "invalid_password")
nil
Link to this function

get_user_by_reset_password_token(token)

View Source
@spec get_user_by_reset_password_token(binary()) :: Zoonk.Accounts.User.t() | nil

Gets the user by reset password token.

examples

Examples

iex> get_user_by_reset_password_token("validtoken")
%User{}

iex> get_user_by_reset_password_token("invalidtoken")
nil
Link to this function

get_user_by_session_token(token)

View Source
@spec get_user_by_session_token(binary()) :: Zoonk.Accounts.User.t()

Gets the user with the given signed token.

Link to this function

get_user_by_username(username)

View Source
@spec get_user_by_username(String.t()) :: Zoonk.Accounts.User.t() | nil

Gets a user by username.

examples

Examples

iex> get_user_by_username("mariecurie")
%User{}

iex> get_user_by_username("unknown")
nil
@spec register_user(map()) :: user_changeset()

Registers a user.

examples

Examples

iex> register_user(%{field: value})
{:ok, %User{}}

iex> register_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
Link to this function

reset_user_password(user, attrs \\ %{})

View Source
@spec reset_user_password(Zoonk.Accounts.User.t(), map()) :: user_changeset()

Resets the user password.

examples

Examples

iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"})
{:ok, %User{}}

iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"})
{:error, %Ecto.Changeset{}}
Link to this function

update_user_email(user, token)

View Source
@spec update_user_email(Zoonk.Accounts.User.t(), String.t()) :: :ok | :error

Updates the user email using the given token.

If the token matches, the user email is updated and the token is deleted. The confirmed_at date is also updated to the current time.

Link to this function

update_user_password(user, password, attrs \\ %{})

View Source
@spec update_user_password(Zoonk.Accounts.User.t(), String.t(), map()) ::
  user_changeset()

Updates the user password.

examples

Examples

iex> update_user_password(user, "valid password", %{password: ...})
{:ok, %User{}}

iex> update_user_password(user, "invalid password", %{password: ...})
{:error, %Ecto.Changeset{}}
Link to this function

update_user_settings(user, attrs \\ %{})

View Source
@spec update_user_settings(Zoonk.Accounts.User.t(), map()) :: user_changeset()

Updates the user settings.

examples

Examples

iex> update_user_settings(user, %{username: ...})
{:ok, %User{}}

iex> update_user_settings(user, %{username: ...})
{:error, %Ecto.Changeset{}}