Add authentication to your mobile app
Turn on authentication in the RevenueCat iOS or Android SDK and send the ID token from your identity provider after each sign-in
This article shows you how to add authentication to your iOS or Android app. You'll turn on authentication in the RevenueCat SDK, listen for authentication events, and send the ID token from your identity provider to RevenueCat after each sign-in. By the end, RevenueCat will identify each Customer from a verified ID token, and your app will hold a RevenueCat access token for that Customer.
Authentication is in private beta, and its API can change before the general release. Tell us about your use case to get early access, and talk to your RevenueCat contact before you use it in a production app.
Prerequisites
- An authentication method in your RevenueCat project for the identity provider that your app uses
- The RevenueCat iOS SDK version 5.88.0 or later, installed in your app, or the RevenueCat Android SDK version 10.29.0 or later, installed in your app
- Sign-in with your identity provider working in your app, so that your app can get an ID token
- Read Authentication for how the identity provider, the method, and the access token fit together
1. Turn on authentication in the SDK
On iOS, import the SDK with @_spi(Internal), then set iamEnabled to true when you configure the SDK. On Android, opt in to InternalRevenueCatAPI, then call iamEnabled(true) on the configuration builder. Every authentication API on Android needs this opt-in.
- Swift
- Kotlin
// Authentication is behind an internal flag during the beta
@_spi(Internal) import RevenueCat
let configuration = Configuration.Builder(withAPIKey: revenueCatAPIKey)
.with(iamEnabled: true)
Purchases.configure(with: configuration.build())
// Authentication is behind an internal flag during the beta
@OptIn(InternalRevenueCatAPI::class)
fun configureRevenueCat(context: Context) {
val configuration = PurchasesConfiguration.Builder(context, revenueCatAPIKey)
.iamEnabled(true)
Purchases.configure(configuration.build())
}
On iOS, if your app uses RevenueCat from app extensions, such as widgets, configure the SDK with .with(iamEnabled: true, keychainAccessGroup: "<YOUR_KEYCHAIN_ACCESS_GROUP>") instead. The SDK stores the session in that keychain access group, so your app and its extensions share the same Customer.
When the SDK initializes, it creates an anonymous Customer and starts a session for that Customer. Each sign-in that follows attaches an identity to the Customer. See Anonymous Customers and identities.
2. Listen for authentication events
Set a delegate on Purchases.shared.authentication on iOS, or Purchases.sharedInstance.authentication on Android, to learn when the SDK updates the access token, and when refreshing it fails. Errors from an explicit login call go to that call instead of the delegate.
- Swift
- Kotlin
The SDK keeps only a weak reference to the delegate. Your app must keep a strong reference to it, or the delegate is released and stops receiving events.
Most apps don't need to read the access token, because the SDK sends it with each of its own requests. Read it when your app calls your own backend, so that the backend can verify the token and identify the Customer. To read the current access token at any time, use currentAccessToken:
- Swift
- Kotlin
let accessToken = Purchases.shared.authentication.currentAccessToken
val accessToken = Purchases.sharedInstance.authentication.currentAccessToken
3. Send the ID token after each sign-in
After your identity provider signs the Customer in, pass its ID token to logIn(using:) on iOS, or awaitLogIn(identity) on Android. On Android, logIn(identity, callback) is available for code that doesn't use coroutines. RevenueCat verifies the token against your authentication method and returns an access token. RevenueCat never receives the Customer's password, only the ID token.
Pick the tab for the provider of your authentication method.
- Apple ID
- Google ID
- Firebase
- OpenID Connect
This example is for iOS only. Apple returns the ID token as Data, so you can pass it to RevenueCat as it is.
- Swift
This example is for Android only. The iOS SDK doesn't support Google ID sign-in yet. It uses Credential Manager to get the Google ID token.
- Kotlin
This example uses the Firebase iOS SDK or the Firebase Android SDK, and signs the Customer in with an email address and a password. Enable the Email/Password provider under Authentication → Sign-in method in the Firebase console first. Every Firebase sign-in method returns the same kind of ID token, so the call to RevenueCat stays the same for the others. To create an account instead, call Auth.auth().createUser(withEmail:password:) on iOS, or createUserWithEmailAndPassword on Android.
- Swift
- Kotlin
A Firebase ID token expires after one hour. getIDToken (getIdToken(false) on Android) refreshes the token first when it is close to its expiration, so call it again for each RevenueCat sign-in instead of storing the token. Password changes, password resets, and email verification stay in Firebase.
This example uses Supabase, with the Supabase Swift SDK or the Supabase Kotlin SDK, and signs the Customer in with an email address and a password. Supabase returns an access token instead of a separate ID token, and that access token is the token to send to RevenueCat. Before you start, make sure that your Supabase project signs its tokens with an asymmetric key. See Example issuer URLs.
- Swift
- Kotlin
To create an account instead, call supabase.auth.signUp(email:password:) on iOS, or supabase.auth.signUpWith(Email) on Android. If your project requires email confirmation, the Customer has no session until they confirm their email address. To read the current token later, use try await supabase.auth.session.accessToken on iOS, or supabase.auth.currentAccessTokenOrNull() on Android. Supabase refreshes the token first when it is close to its expiration, so read it again for each RevenueCat sign-in instead of storing it.
4. Verify the sign-in
Sign in to your app with a test account. The login call returns no error, and currentAccessToken returns an access token.
If an existing Customer already has the identity that you signed in with, RevenueCat merges the anonymous Customer into the existing one. The purchases of both Customers stay with the existing Customer.
Limitations
- Authentication needs iOS SDK version 5.88.0 or later, or Android SDK version 10.29.0 or later. The feature is behind an internal flag. Support for the hybrid SDKs will follow.
- Apple ID sign-in is available on iOS only, and Google ID sign-in is available on Android only.
- Your app must get an ID token from your identity provider before it can authenticate with RevenueCat. RevenueCat does not show a login screen in your app.
Next steps
- Verify access tokens on your backend to protect the calls between your app and your backend
- Spend in-app currency from your app now that the Customer is authenticated
- Authentication for how the identity provider, the method, and the access token fit together