Appearance
Login with phone
To authenticate a user via their phone number, use the Privy's sms
handler. This is a two step process:
- Send an OTP to the user provided phone number.
- Verify the OTP sent to the user.
INFO
Please be sure to configure SMS as a login method on the Privy Developer Dashboard, under "Login Methods".
1. Send an OTP to the user's phone number via SMS
After collecting and validating your users phone number, send an OTP by calling the sendCode
method. Note: you must provide the phone number in E.164 format.
kotlin
val result: Result<Unit> = privy.sms.sendCode(phoneNumber = "
+14155552671")
result.fold(
onSuccess = {
// OTP was successfully sent
},
onFailure = {
println("Error sending OTP: ${it.message}")
}
)
If the OTP is sent successfully, sendCode
will return Result.success
with no associated type. If the provided phone number is invalid, or sending the OTP fails, sendCode
will return Result.failure
.
2. Authenticate with OTP
The user will then receive an SMS with a 6-digit OTP. Prompt for this OTP within your application, then authenticate the user with the loginWithCode
method. Pass the following parameters to this method:
Field | Type | Description |
---|---|---|
code | String | OTP code inputted by the user in your app. |
phoneNumber | String? | (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. |
kotlin
val result: Result<PrivyUser> = privy.sms.loginWithCode(code = "123456", phoneNumber = "+14155552671")
result.fold(
onSuccess = { user ->
// User logged in
},
onFailure = {
println("Error logging in user: ${it.message}")
}
)
If the OTP/phone number combination is valid, Privy will successfully authenticate your user and loginWithCode
will return Result.success
with an encapsulated PrivyUser
. If the provided OTP/phone number combination is invalid, loginWithCode
will return Result.failure
.