Linkhut.Accounts (linkhut v0.1.0)

View Source

The Accounts context.

Summary

Types

An Ecto.Changeset struct for the given data_type.

A username.

Functions

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

Gets a single user by username and verifies the given password matches the stored hash.

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

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

Confirms a user by the given token.

Creates a user.

Checks if the users current e-mail is unconfirmed.

Deletes a user.

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.

Returns the email of a given user.

Gets a single user by its username or user id.

Gets a single user by its username or user id.

Gets a user by email.

Gets the user by reset password token.

Gets the user with the given signed token.

Resets the user password.

Promotes an existing user to admin.

Checks whether the user is in sudo mode.

Updates the user email using the given token.

Updates a user profile.

Types

changeset(data_type)

@type changeset(data_type) :: Ecto.Changeset.t(data_type)

An Ecto.Changeset struct for the given data_type.

username()

@type username() :: binary()

A username.

The types Accounts.username() and binary() are equivalent to analysis tools. Although, for those reading the documentation, Accounts.username() implies a username.

Functions

apply_email_change(user, params)

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

Examples

iex> apply_email_change(user, %{"credential" => %{"email" => email}})
{:ok, %User{}, "current@example.com"}

iex> apply_email_change(user, %{"credential" => %{"email" => email}})
{:error, %Ecto.Changeset{}}

authenticate_by_username_password(username, password)

Gets a single user by username and verifies the given password matches the stored hash.

Examples

iex> authenticate_by_username_password("username", "123456")
{:ok, %User{}}

iex> authenticate_by_username_password("username", "bad_password")
{:error, :unauthorized}

change_credential(credential, attrs \\ %{})

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

Examples

iex> change_credential(credential)
%Ecto.Changeset{data: %Credential{}}

change_user(user, attrs \\ %{})

@spec change_user(Linkhut.Accounts.User.t(), %{optional(any()) => any()}) ::
  changeset(Linkhut.Accounts.User.t())

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

Examples

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

confirm_user(user, token)

Confirms a user by the given token.

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

create_user(attrs \\ %{})

@spec create_user(%{optional(any()) => any()}) ::
  {:ok, Linkhut.Accounts.User.t()}
  | {:error, changeset(Linkhut.Accounts.User.t())}

Creates a user.

Examples

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

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

current_email_unconfirmed?(user)

@spec current_email_unconfirmed?(Linkhut.Accounts.User.t()) :: boolean()

Checks if the users current e-mail is unconfirmed.

delete_user(user, attrs)

@spec delete_user(changeset(Linkhut.Accounts.User.t()), %{required(any()) => any()}) ::
  {:ok, Linkhut.Accounts.User.t()}
  | {:error, changeset(Linkhut.Accounts.User.t())}

Deletes a user.

Examples

iex> delete_user(user, %{"confirmed" => "true"})
{:ok, %User{}}

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

delete_user_session_token(token)

Deletes the signed token with the given context.

deliver_email_confirmation_instructions(user, confirmation_url_fun)

Delivers the confirmation email instructions to the given user.

Examples

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

iex> deliver_email_confirmation_instructions(confirmed_user, &url(~p"/_/confirm/#{&1}"))
{:error, :already_confirmed}

deliver_reset_password_instructions(user, reset_password_url_fun)

Delivers the reset password email to the given user.

Examples

iex> deliver_reset_password_instructions(user, &url(~p"/users/reset-password/#{&1}"))
{:ok, %{to: ..., body: ...}}

deliver_update_email_instructions(user, current_email, confirmation_url_fun)

Delivers the update email instructions to the given user.

Examples

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

generate_user_session_token(user)

Generates a session token.

get_email(user)

@spec get_email(Linkhut.Accounts.User.t()) :: String.t()

Returns the email of a given user.

Examples

iex> get_email(user)
"foo@example.com"

get_user(id)

@spec get_user(integer()) :: Linkhut.Accounts.User.t() | nil
@spec get_user(username()) :: Linkhut.Accounts.User.t() | nil

Gets a single user by its username or user id.

Returns nil if the User doesn't exist.

Examples

iex> get_user!(123)
%User{}

iex> get_user!(456)
nil

get_user!(id)

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

Gets a single user by its username or user id.

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)

@spec get_user_by_email(binary()) :: Linkhut.Accounts.User.t() | nil

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_reset_password_token(token)

Gets the user by reset password token.

Examples

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

iex> get_user_by_reset_password_token("invalidtoken")
nil

get_user_by_session_token(token)

Gets the user with the given signed token.

is_admin?(arg1)

@spec is_admin?(Linkhut.Accounts.User.t()) :: boolean()

reset_user_password(user, attrs)

Resets the user password.

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

set_admin_role(user)

@spec set_admin_role(Linkhut.Accounts.User.t()) ::
  {:ok, Linkhut.Accounts.User.t()}
  | {:error, changeset(Linkhut.Accounts.User.t())}

Promotes an existing user to admin.

Examples

iex> set_admin_role(user)
{:ok, %User{}}

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_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. The confirmed_at date is also updated to the current time.

update_profile(user, attrs)

@spec update_profile(Linkhut.Accounts.User.t(), %{optional(any()) => any()}) ::
  {:ok, Linkhut.Accounts.User.t()}
  | {:error, changeset(Linkhut.Accounts.User.t())}

Updates a user profile.

Examples

iex> update_profile(user, %{field: new_value})
{:ok, %User{}}

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