Linkhut.Accounts (linkhut v0.1.0)
View SourceThe Accounts context.
Summary
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
@type changeset(data_type) :: Ecto.Changeset.t(data_type)
An Ecto.Changeset
struct for the given data_type
.
@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
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{}}
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}
Returns an %Ecto.Changeset{}
for tracking credential changes.
Examples
iex> change_credential(credential)
%Ecto.Changeset{data: %Credential{}}
@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{}}
Confirms a user by the given token.
If the token matches, the user account is marked as confirmed and the token is deleted.
@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{}}
@spec current_email_unconfirmed?(Linkhut.Accounts.User.t()) :: boolean()
Checks if the users current e-mail is unconfirmed.
@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{}}
Deletes the signed token with the given context.
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}
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: ...}}
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: ...}}
Generates a session token.
@spec get_email(Linkhut.Accounts.User.t()) :: String.t()
Returns the email of a given user.
Examples
iex> get_email(user)
"foo@example.com"
@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
@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)
@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
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
Gets the user with the given signed token.
@spec is_admin?(Linkhut.Accounts.User.t()) :: boolean()
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{}}
@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{}}
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.
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.
@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{}}