stein v0.4.1 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:
defmodule MyApp.Users.User do
# ...
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
# ...
end
A sample Ecto migration:
def change() do
create table(:users) do
add(:email, :string)
add(:password_hash, :string)
add(:email_verification_token, :uuid)
add(:email_verified_at, :utc_datetime)
add(:password_reset_token, :uuid)
add(:password_reset_expires_at, :utc_datetime)
timestamps()
end
create index(:users, ["lower(email)"], unique: true)
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
Prepare a user for email validation
Start the password reset process
Trim a field in a changeset if present
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
email()
View Source
email() :: String.t()
email() :: String.t()
password()
View Source
password() :: String.t()
password() :: String.t()
password_hash()
View Source
password_hash() :: String.t()
password_hash() :: String.t()
password_params() View Source
reset_token()
View Source
reset_token() :: String.t()
reset_token() :: String.t()
user()
View Source
user() :: %{
email: email(),
password: password(),
password_hash: password_hash(),
email_verification_token: Stein.uuid(),
email_verified_at: DateTime.t()
}
user() :: %{ email: email(), password: password(), password_hash: password_hash(), email_verification_token: Stein.uuid(), email_verified_at: DateTime.t() }
user_fun()
View Source
user_fun() :: (user() -> :ok)
user_fun() :: (user() -> :ok)
user_schema()
View Source
user_schema() :: atom()
user_schema() :: atom()
Link to this section Functions
email_verified?(user) View Source
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
hash_password(changeset)
View Source
hash_password(Ecto.Changeset.t()) :: Ecto.Changeset.t()
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
reset_password(repo, struct, token, params)
View Source
reset_password(Stein.repo(), user_schema(), reset_token(), password_params()) ::
{:ok, user()} | {:error, Ecto.Changeset.t()}
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
start_email_verification_changeset(changeset)
View Source
start_email_verification_changeset(Ecto.Changeset.t()) :: Ecto.Changeset.t()
start_email_verification_changeset(Ecto.Changeset.t()) :: Ecto.Changeset.t()
Prepare a user for email validation
This should run as part of the create changeset when registering a new user
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_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
, typeutc_datetime
trim_field(changeset, field) View Source
Trim a field in a changeset if present
Calls String.trim/1
on the field and replaces the value.
validate_login(repo, struct, email, password)
View Source
validate_login(Stein.repo(), user_schema(), email(), password()) ::
{:error, :invalid} | {:ok, user()}
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
verify_email(repo, struct, token)
View Source
verify_email(Stein.repo(), user_schema(), Stein.uuid()) ::
{:ok, user()} | {:error, :invalid} | {:error, Ecto.Changeset.t()}
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