View Source Bonfire.Me.Accounts (Bonfire v0.9.11-social-beta.6)

An account represents a private identity within the system, and can have many User identities (see Bonfire.Me.Accounts). An account usually has an Bonfire.Data.Identity.Email and a Bonfire.Data.Identity.Credential user for login.

Summary

Functions

Checks if signup is allowed based on instance config and provided options.

Changes the email for the current account.

Changes the password for the current account.

Returns a changeset for the given changeset name and parameters.

Confirms an account's email address as valid, usually by providing a confirmation token, or directly by providing an Account.

Confirms an account's email manually, by providing the email address. Only for internal or CLI use.

Counts the number of accounts.

Deletes the given account - use enqueue_delete/1 instead.

Enqueues the deletion of the given account.

Fetches the current account by its ID, returns {:error, :not_found} if the ID is nil.

Returns the account by its email.

Returns the current account by its ID or nil if the ID is nil.

Checks if the instance is invite-only.

Checks if the given user or account is an admin.

Attempts to log in by password and either username or email.

Requests an email confirmation for the account.

Requests a password reset to be sent for the account.

Signs up a new account with the given parameters.

Types

changeset_name()

@type changeset_name() :: :change_password | :confirm_email | :login | :signup

Functions

allow_signup?(opts)

Checks if signup is allowed based on instance config and provided options.

Examples

iex> allow_signup?(%{invite: "invite_code"})
true

change_email(current_account, params_or_changeset, opts \\ [])

Changes the email for the current account.

Examples

> change_email(%Account{}, %{old_email: "old@example.com", email: "new@example.com"})
{:ok, %Account{}}

> change_email(%Account{}, %Changeset{valid?: false})
{:error, %Changeset{}}

change_password(current_account, params_or_changeset, opts \\ [])

Changes the password for the current account.

Examples

> change_password(%Account{}, %{old_password: "old", password: "new"})
{:ok, %Account{}}

> change_password(%Account{}, %Changeset{valid?: false})
{:error, %Changeset{}}

change_password(current_account, cs, params, opts)

changeset(changeset_name, params, opts \\ [])

@spec changeset(changeset_name(), params :: map(), opts :: Keyword.t()) ::
  Ecto.Changeset.t()

Returns a changeset for the given changeset name and parameters.

Examples

> changeset(:forgot_password, %{})
%Changeset{}

> changeset(:login, %{email: "test@example.com", password: "secret"})
%Changeset{}

confirm_email(account_or_token, opts \\ [])

Confirms an account's email address as valid, usually by providing a confirmation token, or directly by providing an Account.

Examples

> confirm_email("some_token")
{:ok, %Account{}}

> confirm_email(%Account{})
{:ok, %Account{}}

confirm_email_manually(email)

Confirms an account's email manually, by providing the email address. Only for internal or CLI use.

Examples

> confirm_email_manually("test@example.com")
{:ok, %Account{}}

count()

Counts the number of accounts.

Examples

iex> count()
42

delete(account, opts \\ [])

Deletes the given account - use enqueue_delete/1 instead.

Examples

iex> delete(%Account{})
:ok

iex> delete("some_account_id")
:ok

do_signup(cs_or_params, opts)

enqueue_delete(account)

Enqueues the deletion of the given account.

Examples

> enqueue_delete(%Account{})
:ok

> enqueue_delete("some_account_id")
:ok

fetch_current(id)

Fetches the current account by its ID, returns {:error, :not_found} if the ID is nil.

Examples

iex> fetch_current(nil)
{:error, :not_found}

> fetch_current("some_id")
{:ok, %Account{id: "some_id"}}

get_by_email(email)

Returns the account by its email.

Examples

> get_by_email("test@example.com")
%Account{email: "test@example.com"}

get_current(id)

Returns the current account by its ID or nil if the ID is nil.

Examples

iex> get_current(nil)
nil

> get_current("some_id")
%Account{id: "some_id"}

instance_is_invite_only?()

Checks if the instance is invite-only.

Examples

> instance_is_invite_only?()
true

is_admin?(assigns)

Checks if the given user or account is an admin.

Examples

> is_admin?(user)
true

> is_admin?(account)
true

is_first_account?()

login(params_or_changeset, opts \\ [])

Attempts to log in by password and either username or email.

Accepts a map of parameters or a LoginFields changeset.

On success, returns {:ok, account, user} if a username was provided and {:ok, account, nil} otherwise. On error, returns {:error, changeset}

Examples

> login(%{email: "test@example.com", password: "secret"})
{:ok, %Account{}, nil}

> login(%{username: "test", password: "secret"})
{:ok, %Account{}, %User{}}

> login(%Changeset{valid?: false})
{:error, %Changeset{}}

login_valid?(user, password)

make_account(attrs \\ %{}, opts \\ [])

maybe_redeem_invite(data, opts)

prepare_signup_opts(opts)

redeemable_invite?(invite)

request_confirm_email(params_or_changeset_or_form_or_account, opts \\ [])

Requests an email confirmation for the account.

Examples

> request_confirm_email(%{email: "test@example.com"})
{:ok, :resent, %Account{}}

> request_confirm_email(%Changeset{valid?: false})
{:error, %Changeset{}}

request_forgot_password(params)

Requests a password reset to be sent for the account.

Examples

> request_forgot_password(%{email: "test@example.com"})
{:ok, :resent, %Account{}}

signup(params_or_changeset, opts \\ [])

Signs up a new account with the given parameters.

Examples

> signup(%{email: "test@example.com", password: "secret"})
{:ok, %Account{}}

> signup(%Changeset{valid?: false})
{:error, %Changeset{}}

update_is_admin(user_or_account, make_admin_or_revoke, user \\ nil)