SMS
Privy enables users to login to your application with SMS or email. With Privy, your application can verify ownership of a user’s email address or phone number to send them notifications, campaigns, and more to keep them activated.
To authenticate your users with a one-time passcode (OTP) sent to their phone number, use the useLoginWithSms
hook.
Send Code
Parameters
The phone number of the user to log in. Must follow specific formatting conventions (see below).
Whether to disable the ability to sign up with the phone number.
Returns
A promise that resolves when the code is sent.
Formatting the phone number
The sendCode
method requires a phoneNumber
string param that must follow these formatting conventions:
- By default, the implicit phone number country code is +1/US.
- Explicitly prepending a
(+)1
to the phone number will still be read as a US phone number. - For non-US phone numbers, append a
+${countryCode}
to the beginning of the input value. - Non-numerical values in the string are ignored, except for a leading
+
that denotes a custom country code.
Login with Code
Parameters
The one-time passcode sent to the user’s phone number.
Returns
A promise that resolves when the user is logged in.
Usage
Tracking Flow State
Track the state of the OTP flow via the state
variable returned by the
useLoginWithSms
hook.
The current state of the OTP flow.
The error that occurred during the OTP flow.
Callbacks
You can optionally pass callbacks into the useLoginWithSms
hook to run custom logic after a successful login or to handle errors that occur during the flow.
onComplete
Parameters
The user object corresponding to the authenticated user.
Whether the user is a new user or an existing user.
Whether the user entered the application already authenticated.
The method used by the user to login.
The account corresponding to the loginMethod used.
onError
Parameters
The error that occurred during the login flow.
To authenticate your users with a one-time passcode (OTP) sent to their phone number, use the useLoginWithSms
hook.
Send Code
Parameters
The phone number of the user to log in. Must follow specific formatting conventions (see below).
Whether to disable the ability to sign up with the phone number.
Returns
A promise that resolves when the code is sent.
Formatting the phone number
The sendCode
method requires a phoneNumber
string param that must follow these formatting conventions:
- By default, the implicit phone number country code is +1/US.
- Explicitly prepending a
(+)1
to the phone number will still be read as a US phone number. - For non-US phone numbers, append a
+${countryCode}
to the beginning of the input value. - Non-numerical values in the string are ignored, except for a leading
+
that denotes a custom country code.
Login with Code
Parameters
The one-time passcode sent to the user’s phone number.
Returns
A promise that resolves when the user is logged in.
Usage
Tracking Flow State
Track the state of the OTP flow via the state
variable returned by the
useLoginWithSms
hook.
The current state of the OTP flow.
The error that occurred during the OTP flow.
Callbacks
You can optionally pass callbacks into the useLoginWithSms
hook to run custom logic after a successful login or to handle errors that occur during the flow.
onComplete
Parameters
The user object corresponding to the authenticated user.
Whether the user is a new user or an existing user.
Whether the user entered the application already authenticated.
The method used by the user to login.
The account corresponding to the loginMethod used.
onError
Parameters
The error that occurred during the login flow.
To authenticate your users with a one-time passcode (OTP) sent to their phone number, use the useLoginWithSMS
hook.
Send Code
Parameters
The phone number of the user to log in.
Returns
A promise that resolves to an object with a success property indicating if the code was sent successfully.
Login with Code
Parameters
The one-time passcode sent to the user’s phone number.
The user’s phone number. Though this parameter is optional, it is highly recommended that you pass the user’s phone number explicitly.
Whether to disable the ability to sign up with the phone number.
Returns
The user object returned by the login process.
Usage
Tracking login flow state
The state variable returned from useLoginWithSMS will always be one of the following values.
The current state of the SMS login flow.
The error that occurred during the SMS login flow.
Callbacks
You can optionally pass callbacks into the useLoginWithSMS
hook to run custom logic after an OTP has been sent, after a successful login, or to handle errors that occur during the flow.
onSendCodeSuccess
Parameters
The phone number the code was sent to.
onLoginSuccess
Parameters
The PrivyUser returned by loginWithCode.
Whether the user is a new user or an existing user.
onError
Parameters
The error that occurred during the login flow.
To authenticate a user via their phone number, use the Privy client’s sms
handler.
Send Code
Parameters
The phone number of the user to log in. Must be in E.164 format (e.g., “+14155552671”).
Returns
Nothing, indicating success.
Throws
An error if sending the code fails.
Login with Code
Parameters
The one-time passcode sent to the user’s phone number.
The user’s phone number.
Returns
The authenticated Privy user
Throws
An error if logging the user in is unsuccessful.
Usage
To authenticate a user via their phone number, use the Privy client’s sms
handler.
Send Code
Parameters
The phone number of the user to log in. Must be in E.164 format (e.g., “+14155552671”).
Returns
A Result object that indicates whether the operation was successful. Returns Result.success if the code was sent successfully, or Result.failure if there was an error.
Login with Code
Parameters
The one-time passcode sent to the user’s phone number.
(Optional) The user’s phone number. Though this parameter is optional, it is highly recommended that you pass the user’s phone number explicitly. If phone number is omitted, the phone number from sendCode will be used.
Returns
A Result object containing the PrivyUser if successful, or an error if the operation failed.
Usage
To authenticate a user via their phone number, use the Privy client’s sms
handler.
Send Code
Parameters
The phone number of the user to log in. Must be in E.164 format (e.g., “+14155552671”).
Returns
A Result object that indicates whether the operation was successful. Returns Result.success if the code was sent successfully, or Result.failure if there was an error.
Login with Code
Parameters
The one-time passcode sent to the user’s phone number.
(Optional) The user’s phone number. Though this parameter is optional, it is highly recommended that you pass the user’s phone number explicitly. If phone number is omitted, the phone number from sendCode will be used.
Returns
A Result object containing the PrivyUser if successful, or an error if the operation failed.