Phone (SMS) login

Phone authentication lets users create accounts using their phone numbers and log in through SMS messages.

Create and use mock phone numbers to initiate a phone authentication process without an actual phone number.

Send SMS message

Phone authentication is done using a two-step authentication process. When using phone authentication, the authentication request is initiated from the client application and an SMS message is sent to the user's phone. The SMS message will contain a secret the user can use to log in.

Send an SMS message to initiate the authentication process. A new account will be created for this phone number if it has never been used before.

import { Client, Account, ID } from "appwrite";

const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

const account = new Account(client);

const token = await account.createPhoneToken(
    ID.unique(),
    '+14255550123'
);

const userId = token.userId;

Login

After initiating the phone authentication process, the returned user ID and secret are used to confirm the user. The secret will usually be a 6-digit number in the SMS message sent to the user.

import { Client, Account, ID } from "appwrite";

const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

const account = new Account(client);

const session = await account.createSession(
    userId,
    '[SECRET]'
);

After the secret is verified, a session will be created.