View Source Appwrite.Services.Accounts (appwrite v0.2.1)

The Account service allows you to authenticate and manage a user account. You can use the account service to update user information, retrieve the user sessions across different devices, and fetch the user security logs with their recent activity. You can authenticate the user account by using multiple sign-in methods available. Once the user is authenticated, a new session object will be created to allow the user to access their private data and settings.

Summary

Functions

Create an anonymous session.

Create an email-password session.

Create an email token for passwordless / magic-link authentication.

Create a JWT for the current user.

Create a magic URL token for passwordless authentication.

Create a new MFA authenticator.

Create an MFA challenge.

Generate recovery codes for MFA.

Create a phone token for SMS-based authentication.

Create phone verification.

Create a password recovery request for the user.

Create a session from a token.

Create email verification.

Delete an identity by its unique ID.

Delete an MFA authenticator.

Delete a push notification target.

Delete a session by its ID.

Delete all active sessions for the currently logged-in user.

Get the currently logged-in user.

Get recovery codes that can be used as a backup for the MFA flow.

Get the preferences of the currently logged-in user.

Get a session by its ID.

List identities for the currently logged-in user.

List logs for the currently logged-in user.

List the MFA factors available on the account.

List all active sessions for the currently logged-in user.

Update the currently logged-in user's email address.

Update a magic URL session (complete the magic-link authentication flow).

Update MFA status for the account.

Verify and enable an MFA authenticator.

Complete an MFA challenge by providing the one-time password (OTP).

Regenerate recovery codes for MFA. Requires a completed OTP challenge.

Update the currently logged-in user's name.

Update the currently logged-in user's password.

Update the currently logged-in user's phone number.

Update phone session (complete the phone/OTP authentication flow).

Update (complete) phone verification.

Update the preferences of the currently logged-in user.

Update a push notification target.

Update (extend) a session.

Block the currently logged-in user account.

Update (complete) email verification.

Functions

create(user_id \\ nil, email, password, name \\ nil)

@spec create(String.t() | nil, String.t(), String.t(), String.t() | nil) ::
  {:ok, Appwrite.Types.User.t()} | {:error, any()}

Create a new account.

Parameters

  • user_id: The unique ID for the new user. Auto-generated if nil.
  • email: The email address of the user.
  • password: The user's password.
  • name: (Optional) The user's name.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

create_anonymous_session()

@spec create_anonymous_session() ::
  {:ok, Appwrite.Types.Session.t()} | {:error, any()}

Create an anonymous session.

Registers an anonymous account in your project and creates a new session for the user.

Returns

  • {:ok, Session.t()} on success
  • {:error, reason} on failure

create_email_password_session(email, password)

@spec create_email_password_session(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Session.t()} | {:error, any()}

Create an email-password session.

Allows the user to log in with an email and password combination.

Parameters

  • email (required): The user's email.
  • password (required): The user's password.

Returns

  • {:ok, Session.t()} on success
  • {:error, reason} on failure

create_email_token(user_id, email, phrase \\ nil)

@spec create_email_token(String.t(), String.t(), boolean() | nil) ::
  {:ok, Appwrite.Types.Token.t()}
  | {:error, Appwrite.Exceptions.AppwriteException.t()}

Create an email token for passwordless / magic-link authentication.

Sends the user an email with a secret key for creating a session.

Parameters

  • user_id (required): The user ID.
  • email (required): The email address.
  • phrase (optional): When true, a security phrase is included in the email.

Returns

  • {:ok, Token.t()} on success
  • {:error, AppwriteException.t()} on failure

create_jwt()

@spec create_jwt() :: {:ok, Appwrite.Types.Jwt.t()} | {:error, any()}

Create a JWT for the current user.

Returns

  • {:ok, Jwt.t()} on success
  • {:error, reason} on failure

create_magic_url_token(user_id, email, url \\ nil, phrase \\ nil)

@spec create_magic_url_token(
  String.t(),
  String.t(),
  String.t() | nil,
  boolean() | nil
) ::
  {:ok, Appwrite.Types.Token.t()}
  | {:error, Appwrite.Exceptions.AppwriteException.t()}

Create a magic URL token for passwordless authentication.

Sends the user an email with a magic link for logging in.

Parameters

  • user_id (required): The user ID.
  • email (required): The email address.
  • url (optional): Redirect URL after authentication.
  • phrase (optional): When true, a security phrase is included in the email.

Returns

  • {:ok, Token.t()} on success
  • {:error, AppwriteException.t()} on failure

create_mfa_authenticator(type)

@spec create_mfa_authenticator(String.t()) ::
  {:ok, Appwrite.Types.MfaType.t()} | {:error, any()}

Create a new MFA authenticator.

Parameters

  • type: The type of authenticator (e.g. "totp").

Returns

  • {:ok, MfaType.t()} on success
  • {:error, reason} on failure

create_mfa_challenge(factor)

@spec create_mfa_challenge(String.t()) ::
  {:ok, Appwrite.Types.MfaChallenge.t()} | {:error, any()}

Create an MFA challenge.

Parameters

  • factor: The MFA factor to challenge. Must be a valid AuthenticationFactor value.

Returns

  • {:ok, MfaChallenge.t()} on success
  • {:error, reason} on failure

create_mfa_recovery_codes()

@spec create_mfa_recovery_codes() ::
  {:ok, Appwrite.Types.MfaRecoveryCodes.t()} | {:error, any()}

Generate recovery codes for MFA.

Returns

  • {:ok, MfaRecoveryCodes.t()} on success
  • {:error, reason} on failure

create_oauth2_session(provider, success \\ nil, failure \\ nil, scopes \\ nil)

@spec create_oauth2_session(
  String.t(),
  String.t() | nil,
  String.t() | nil,
  [String.t()] | nil
) :: {:ok, String.t()} | {:error, any()}

Create an OAuth2 session.

Logs the user in with an OAuth2 provider. Returns the OAuth2 authorization URL. Redirect the user to this URL to complete authentication.

Parameters

  • provider (required): The OAuth2 provider name (e.g. "google", "github").
  • success (optional): URL to redirect on successful authentication.
  • failure (optional): URL to redirect on authentication failure.
  • scopes (optional): List of requested OAuth2 scopes.

Returns

  • {:ok, String.t()} containing the OAuth2 authorization URL on success
  • {:error, reason} on failure

create_oauth2_token(provider, success \\ nil, failure \\ nil, scopes \\ nil)

@spec create_oauth2_token(
  String.t(),
  String.t() | nil,
  String.t() | nil,
  [String.t()] | nil
) :: {:ok, String.t()} | {:error, Appwrite.Exceptions.AppwriteException.t()}

Create an OAuth2 token URL.

Builds an OAuth2 authorization URL. Redirect the user to this URL to begin the OAuth2 flow. On success, userId and secret are appended to the success URL as query parameters, which can then be used with create_session/2.

Parameters

  • provider (required): OAuth2 provider name (e.g. "google", "github").
  • success (optional): Success redirect URL.
  • failure (optional): Failure redirect URL.
  • scopes (optional): List of OAuth2 scopes to request.

Returns

  • {:ok, String.t()} containing the authorization URL on success
  • {:error, AppwriteException.t()} on failure

create_phone_token(user_id, phone)

@spec create_phone_token(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Token.t()}
  | {:error, Appwrite.Exceptions.AppwriteException.t()}

Create a phone token for SMS-based authentication.

Sends the user an SMS with a secret key for creating a session.

Parameters

  • user_id (required): The user ID.
  • phone (required): The phone number in E.164 format.

Returns

  • {:ok, Token.t()} on success
  • {:error, AppwriteException.t()} on failure

create_phone_verification()

@spec create_phone_verification() :: {:ok, Appwrite.Types.Token.t()} | {:error, any()}

Create phone verification.

Sends a verification SMS to the currently logged-in user's phone number. After the user verifies, use update_phone_verification/2 to complete the flow.

Returns

  • {:ok, Token.t()} on success
  • {:error, reason} on failure

create_push_target(target_id, identifier, provider_id \\ nil)

@spec create_push_target(String.t(), String.t(), String.t() | nil) ::
  {:ok, Appwrite.Types.Target.t()} | {:error, any()}

Create a push notification target.

Parameters

  • target_id (required): Unique target identifier.
  • identifier (required): The push token / device identifier.
  • provider_id (optional): The messaging provider ID.

Returns

  • {:ok, Target.t()} on success
  • {:error, reason} on failure

create_recovery(email, url)

@spec create_recovery(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Token.t()} | {:error, any()}

Create a password recovery request for the user.

Parameters

  • email (required): The user's email address.
  • url (required): The URL to redirect the user to after the password reset.

Returns

  • {:ok, Token.t()} on success
  • {:error, reason} on failure

create_session(user_id, secret)

@spec create_session(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Session.t()} | {:error, any()}

Create a session from a token.

Creates a session using the user ID and secret from a token-based authentication flow (e.g. after create_email_token/3 or create_phone_token/2).

Parameters

  • user_id (required): The user ID.
  • secret (required): The secret token.

Returns

  • {:ok, Session.t()} on success
  • {:error, reason} on failure

create_verification(url)

@spec create_verification(String.t()) ::
  {:ok, Appwrite.Types.Token.t()} | {:error, any()}

Create email verification.

Sends a verification email to the currently logged-in user's email address. After the user verifies via the link, use update_verification/2 to complete the flow.

Parameters

  • url (required): The redirect URL embedded in the verification email.

Returns

  • {:ok, Token.t()} on success
  • {:error, reason} on failure

delete_identity(identity_id)

@spec delete_identity(String.t()) :: {:ok, map()} | {:error, any()}

Delete an identity by its unique ID.

Parameters

  • identity_id: The ID of the identity to delete.

Returns

  • {:ok, map()} on success
  • {:error, reason} on failure

delete_mfa_authenticator(type)

@spec delete_mfa_authenticator(String.t()) :: {:ok, map()} | {:error, any()}

Delete an MFA authenticator.

Parameters

  • type: The type of authenticator to delete.

Returns

  • {:ok, map()} on success
  • {:error, reason} on failure

delete_push_target(target_id)

@spec delete_push_target(String.t()) ::
  {:ok, map()} | {:error, Appwrite.Exceptions.AppwriteException.t()}

Delete a push notification target.

Parameters

  • target_id (required): The target identifier.

Returns

  • {:ok, map()} on success
  • {:error, AppwriteException.t()} on failure

delete_session(session_map, session_id)

@spec delete_session(%{required(String.t()) => String.t()}, String.t()) ::
  {:ok, map()} | {:error, any()}

Delete a session by its ID.

Parameters

  • session_map: Headers map, e.g. %{"X-Appwrite-Session" => session_token}.
  • session_id (required): The session ID. Use "current" for the active session.

Returns

  • {:ok, map()} on success
  • {:error, reason} on failure

delete_sessions()

@spec delete_sessions() :: {:ok, map()} | {:error, any()}

Delete all active sessions for the currently logged-in user.

Returns

  • {:ok, map()} on success
  • {:error, reason} on failure

get()

@spec get() :: {:ok, Appwrite.Types.User.t()} | {:error, any()}

Get the currently logged-in user.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

get_mfa_recovery_codes()

@spec get_mfa_recovery_codes() ::
  {:ok, Appwrite.Types.MfaRecoveryCodes.t()} | {:error, any()}

Get recovery codes that can be used as a backup for the MFA flow.

Returns

  • {:ok, MfaRecoveryCodes.t()} on success
  • {:error, reason} on failure

get_prefs()

@spec get_prefs() :: {:ok, Appwrite.Types.Preference.t()} | {:error, any()}

Get the preferences of the currently logged-in user.

Returns

  • {:ok, Preference.t()} on success
  • {:error, reason} on failure

get_session(session_map, session_id)

@spec get_session(%{required(String.t()) => String.t()}, String.t()) ::
  {:ok, Appwrite.Types.Session.t()} | {:error, any()}

Get a session by its ID.

Parameters

  • session_map: Headers map, e.g. %{"X-Appwrite-Session" => session_token}.
  • session_id (required): The session ID. Use "current" for the active session.

Returns

  • {:ok, Session.t()} on success
  • {:error, reason} on failure

list_identities(queries \\ nil)

@spec list_identities([String.t()] | nil) ::
  {:ok, Appwrite.Types.IdentityList.t()} | {:error, any()}

List identities for the currently logged-in user.

Parameters

  • queries: (Optional) A list of query strings.

Returns

  • {:ok, IdentityList.t()} on success
  • {:error, reason} on failure

list_logs(queries \\ nil)

@spec list_logs([String.t()] | nil) ::
  {:ok, Appwrite.Types.LogList.t()} | {:error, any()}

List logs for the currently logged-in user.

Parameters

  • queries: (Optional) A list of query strings.

Returns

  • {:ok, LogList.t()} on success
  • {:error, reason} on failure

list_mfa_factors()

@spec list_mfa_factors() :: {:ok, Appwrite.Types.MfaFactors.t()} | {:error, any()}

List the MFA factors available on the account.

Returns

  • {:ok, MfaFactors.t()} on success
  • {:error, reason} on failure

list_sessions()

@spec list_sessions() :: {:ok, Appwrite.Types.SessionList.t()} | {:error, any()}

List all active sessions for the currently logged-in user.

Returns

  • {:ok, SessionList.t()} on success
  • {:error, reason} on failure

update_email(email, password)

@spec update_email(String.t(), String.t()) ::
  {:ok, Appwrite.Types.User.t()} | {:error, any()}

Update the currently logged-in user's email address.

After changing the email, the user's confirmation status is reset. A new confirmation email is not sent automatically — use create_verification/1 to resend. Requires the user's current password for security.

This endpoint can also be used to convert an anonymous account to a normal one by providing an email address and a new password.

Note

Set the session token in the request header before calling this function.

Parameters

  • email: The new email address.
  • password: The user's current password.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

update_magic_url_session(user_id, secret)

@spec update_magic_url_session(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Session.t()} | {:error, any()}

Update a magic URL session (complete the magic-link authentication flow).

Creates a session using the user ID and secret from a magic-URL token flow.

Parameters

  • user_id (required): The user ID.
  • secret (required): The secret token from the magic URL.

Returns

  • {:ok, Session.t()} on success
  • {:error, reason} on failure

update_mfa(mfa)

@spec update_mfa(boolean()) :: {:ok, Appwrite.Types.User.t()} | {:error, any()}

Update MFA status for the account.

Parameters

  • mfa: Boolean to enable (true) or disable (false) MFA.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

update_mfa_authenticator(type, otp)

@spec update_mfa_authenticator(String.t(), String.t()) ::
  {:ok, Appwrite.Types.User.t()} | {:error, any()}

Verify and enable an MFA authenticator.

Parameters

  • type: The type of authenticator.
  • otp: The one-time password to verify.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

update_mfa_challenge(challenge_id, otp)

@spec update_mfa_challenge(String.t(), String.t()) :: {:ok, map()} | {:error, any()}

Complete an MFA challenge by providing the one-time password (OTP).

To begin the MFA flow, use create_mfa_challenge/1.

Parameters

  • challenge_id (required): The ID of the challenge.
  • otp (required): The one-time password.

Returns

  • {:ok, map()} on success
  • {:error, reason} on failure

update_mfa_recovery_codes()

@spec update_mfa_recovery_codes() ::
  {:ok, Appwrite.Types.MfaRecoveryCodes.t()} | {:error, any()}

Regenerate recovery codes for MFA. Requires a completed OTP challenge.

Returns

  • {:ok, MfaRecoveryCodes.t()} on success
  • {:error, reason} on failure

update_name(name)

@spec update_name(String.t()) :: {:ok, Appwrite.Types.User.t()} | {:error, any()}

Update the currently logged-in user's name.

Parameters

  • name (required): The new name of the user.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

update_password(new_password, old_password \\ nil)

@spec update_password(String.t(), String.t() | nil) ::
  {:ok, Appwrite.Types.User.t()} | {:error, any()}

Update the currently logged-in user's password.

Parameters

  • new_password (required): The new password for the user.
  • old_password (optional): The user's current password. Required when the user has an existing password.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

update_phone(phone, password)

@spec update_phone(String.t(), String.t()) ::
  {:ok, Appwrite.Types.User.t()} | {:error, any()}

Update the currently logged-in user's phone number.

Parameters

  • phone (required): The new phone number in E.164 format.
  • password (required): The user's current password.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

update_phone_session(user_id, secret)

@spec update_phone_session(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Session.t()} | {:error, any()}

Update phone session (complete the phone/OTP authentication flow).

Creates a session using the user ID and secret from a phone-based authentication flow.

Parameters

  • user_id (required): The user ID.
  • secret (required): The secret token from the phone token.

Returns

  • {:ok, Session.t()} on success
  • {:error, reason} on failure

update_phone_verification(user_id, secret)

@spec update_phone_verification(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Token.t()} | {:error, any()}

Update (complete) phone verification.

Completes the user phone verification process using the userId and secret from the verification SMS.

Parameters

  • user_id (required): The user's ID.
  • secret (required): The secret key from the verification SMS.

Returns

  • {:ok, Token.t()} on success
  • {:error, reason} on failure

update_prefs(prefs)

@spec update_prefs(Appwrite.Types.Preference.t()) ::
  {:ok, Appwrite.Types.User.t()} | {:error, any()}

Update the preferences of the currently logged-in user.

The object you pass is stored as-is and replaces any previous preferences.

Parameters

  • prefs (required): A string-keyed map of user preferences.

Returns

  • {:ok, User.t()} on success
  • {:error, reason} on failure

update_push_target(target_id, identifier)

@spec update_push_target(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Target.t()} | {:error, any()}

Update a push notification target.

Parameters

  • target_id (required): The target identifier.
  • identifier (required): The new push token / device identifier.

Returns

  • {:ok, Target.t()} on success
  • {:error, reason} on failure

update_session(session_map, session_id)

@spec update_session(%{required(String.t()) => String.t()}, String.t()) ::
  {:ok, Appwrite.Types.Session.t()} | {:error, any()}

Update (extend) a session.

Refreshes the session to extend its lifetime. If the session was created using an OAuth provider, this also refreshes the provider access token.

Parameters

  • session_map: Headers map, e.g. %{"X-Appwrite-Session" => session_token}.
  • session_id (required): The ID of the session to update.

Returns

  • {:ok, Session.t()} on success
  • {:error, reason} on failure

update_status()

@spec update_status() ::
  {:ok, Appwrite.Types.User.t()}
  | {:error, Appwrite.Exceptions.AppwriteException.t()}

Block the currently logged-in user account.

Sets the user's status to disabled. The user record is not deleted but the account is blocked from access. Returns the updated user object.

Returns

  • {:ok, User.t()} on success
  • {:error, AppwriteException.t()} on failure

update_verification(user_id, secret)

@spec update_verification(String.t(), String.t()) ::
  {:ok, Appwrite.Types.Token.t()} | {:error, any()}

Update (complete) email verification.

Completes the user email verification process using the userId and secret from the verification link.

Parameters

  • user_id (required): The user's ID.
  • secret (required): The secret key from the verification link.

Returns

  • {:ok, Token.t()} on success
  • {:error, reason} on failure