Jumar.Accounts (Jumar v0.1.0)

View Source

The Accounts context.

Summary

Functions

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

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

Deletes the signed token with the given context.

Delivers the magic link login instructions 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 email and password.

Gets the user with the given magic link token.

Gets the user with the given signed token.

Logs the user in by magic link.

Registers a user.

Checks whether the user is in sudo mode.

Updates the user email using the given token.

Updates the user password.

Functions

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

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

See Jumar.Accounts.User.email_changeset/3 for a list of supported options.

Examples

iex> change_user_email(user)
%Ecto.Changeset{data: %User{}}

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

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

See Jumar.Accounts.User.password_changeset/3 for a list of supported options.

Examples

iex> change_user_password(user)
%Ecto.Changeset{data: %User{}}

delete_user_session_token(token)

Deletes the signed token with the given context.

deliver_login_instructions(user, magic_link_url_fun)

Delivers the magic link login instructions to the given user.

deliver_user_update_email_instructions(user, current_email, update_email_url_fun)

Delivers the update email instructions to the given user.

Examples

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

generate_user_session_token(user)

Generates a session token.

get_user!(id)

Gets a single user.

Raises Ecto.NoResultsError if the User does not exist.

Examples

iex> get_user!(123)
%User{}

iex> get_user!(456)
** (Ecto.NoResultsError)

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_email_and_password(email, password)

Gets a user by email and password.

Examples

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

iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
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.

login_user_by_magic_link(token)

Logs the user in by magic link.

There are three cases to consider:

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

  2. The user has not confirmed their email and no password is set. 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.

  3. The user has not confirmed their email but a password is set. This cannot happen in the default implementation but may be the source of security pitfalls. See the "Mixing magic link and password registration" section of mix help phx.gen.auth.

register_user(attrs)

Registers a user.

Examples

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

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

sudo_mode?(user, minutes \\ -20)

Checks whether the user is in sudo mode.

The user is in sudo mode when the last authentication was done no further than 20 minutes ago. The limit can be given as second argument in minutes.

update_user_email(user, token)

Updates the user email using the given token.

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

update_user_password(user, attrs)

Updates the user password.

Returns a tuple with the updated user, as well as a list of expired tokens.

Examples

iex> update_user_password(user, %{password: ...})
{:ok, {%User{}, [...]}}

iex> update_user_password(user, %{password: "too short"})
{:error, %Ecto.Changeset{}}