stein v0.2.0 Stein.Accounts View Source

Helper functions around user accounts

To fully utilize the Stein.Accounts functions, your user schema struct should contain the following fields:

schema "users" do
  field(:email, :string)
  field(:password, :string, virtual: true)
  field(:password_hash, :string)

  field(:email_verification_token, Ecto.UUID)
  field(:email_verified_at, :utc_datetime)

  field(:password_reset_token, Ecto.UUID)
  field(:password_reset_expires_at, :utc_datetime)
end

Link to this section Summary

Functions

Check if the user's email has been verified

Hash the changed password in a changeset

Finish resetting a password

Validate a email and password match a user

Verify a user's email address from a token sent to their email address

Link to this section Types

Link to this type

password() View Source
password() :: String.t()

Link to this type

password_hash() View Source
password_hash() :: String.t()

Link to this type

password_params() View Source
password_params() :: %{password: password(), password_confirmation: password()}

Link to this type

reset_token() View Source
reset_token() :: String.t()

Link to this type

user() View Source
user() :: %{
  email: email(),
  password: password(),
  password_hash: password_hash(),
  email_verification_token: Stein.uuid(),
  email_verified_at: DateTime.t()
}

Link to this type

user_fun() View Source
user_fun() :: (user() -> :ok)

Link to this type

user_schema() View Source
user_schema() :: atom()

Link to this section Functions

Link to this function

email_verified?(user) View Source
email_verified?(user()) :: boolean()

Check if the user's email has been verified

iex> user = %User{email_verified_at: Timex.now()}
iex> Accounts.email_verified?(user)
true

iex> user = %User{}
iex> Accounts.email_verified?(user)
false
Link to this function

hash_password(changeset) View Source
hash_password(Ecto.Changeset.t()) :: Ecto.Changeset.t()

Hash the changed password in a changeset

  • Skips if the changeset is invalid
  • Skips if a password is not changed
  • Hashes the password with BCrypt otherwise

Requires the user schema to contain:

  • password, type :string
  • password_hash, type :string
Link to this function

reset_password(repo, struct, token, params) View Source
reset_password(Stein.repo(), user_schema(), reset_token(), password_params()) ::
  {:ok, user()} | {:error, Ecto.Changeset.t()}

Finish resetting a password

Takes the token, checks for expiration, and then resets the password

Link to this function

start_password_reset(repo, struct, email, success_fun \\ fn _user -> :ok end) View Source
start_password_reset(Stein.repo(), user_schema(), email(), user_fun()) :: :ok

Start the password reset process

On successful start of reset, the success function will be called. This can be used to send the password reset email.

Requires the user schema to contain:

  • password_reset_token, type :uuid
  • password_reset_expires_at, type utc_datetime
Link to this function

validate_login(repo, struct, email, password) View Source
validate_login(Stein.repo(), user_schema(), email(), password()) ::
  {:error, :invalid} | {:ok, user()}

Validate a email and password match a user

Requires the user schema to contain:

  • email, type :string
  • password_hash, type :string
Link to this function

verify_email(repo, struct, token) View Source
verify_email(Stein.repo(), user_schema(), Stein.uuid()) ::
  {:ok, user()} | {:error, :invalid} | {:error, Ecto.Changeset.t()}

Verify a user's email address from a token sent to their email address

This token should be a UUID, if it is not {:error, :invalid} will be returned.

Requires the user schema to contain:

  • email_verification_token, type :uuid
  • email_verified_at, type :utc_datetime