Skip to content

Instantly share code, notes, and snippets.

@jan-tennert
Created September 24, 2023 09:22
Show Gist options
  • Save jan-tennert/c624ff9d056f61dba849f0956180cec7 to your computer and use it in GitHub Desktop.
Save jan-tennert/c624ff9d056f61dba849f0956180cec7 to your computer and use it in GitHub Desktop.
Phone Authentication with Kotlin

Phone Authentication with Kotlin

Phone Authentication is another method of Authentication in Supabase. The Kotlin library provides a easy way to integrate Phone Auth in your App.

Setting up a SMS provider

Before you can try out Phone Auth, you have to setup a SMS provider in your Supabase Dashboard. There you can also customize the SMS message and the OTP, the user receives.

Signing up with a Phone number

After you setup everything, you can easily create new users with a phone number:

val user = client.gotrue.signUpWith(Phone) {
    phoneNumber = "+12345678912"
    password = "password"
}

If you enabled phone confirmations, the user should receive a phone message with an OTP. You can use this OTP to confirm the sign up and login:

client.gotrue.verifyPhoneOtp(
    type = OtpType.Phone.SMS,
    phoneNumber = "+123456789",
    token = "123456"
)

If this was successful, the user is logged in!

Logging in with a Phone number

Passwordless SMS login

Phone Authentication allows passwordless login, allowing users to login with just their phone number:

client.gotrue.sendOtpTo(Phone) {
    phoneNumber = "+123456789"
}

Once received, verify the OTP:

client.gotrue.verifyPhoneOtp(
    type = OtpType.Phone.SMS,
    phoneNumber = "+123456789",
    token = "123456"
)

Phone login with Password

Phone login with a password can also be done:

client.gotrue.loginWith(Phone) {
    phoneNumber = "+1234567891"
    password = "password"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment