Appearance
Login with email
To authenticate a user via their email address, use the Privy's email
handler. This is a two step process:
- Send an OTP to the user provided email address.
- Verify the OTP sent to the user.
INFO
Please be sure to configure email as a login method on the Privy Developer Dashboard, under "Login Methods".
1. Send an OTP to the user's email address
To login a user by their email address, first send an OTP by calling the sendCode
method.
kotlin
val result: Result<Unit> = privy.email.sendCode(email = "[email protected]")
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 email address 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. |
email | String? | (Optional) The user's email address. Though this parameter is optional, it is highly recommended that you pass the user's email address explicitly. If email is omitted, the email from sendCode will be used. |
kotlin
val result: Result<PrivyUser> = privy.email.loginWithCode(code = "123456", email = "[email protected]")
result.fold(
onSuccess = { user ->
// User logged in
},
onFailure = {
println("Error logging in user: ${it.message}")
}
)
If the OTP/email combination is valid, Privy will successfully authenticate your user and loginWithCode
will return Result.success
with an encapsulated PrivyUser
. If the provided OTP/email combination is invalid, loginWithCode
will return Result.failure
.