Skip to content

Login with OAuth

To authenticate a user via an OAuth account (e.g. Google), use the Privy client's oAuth handler.

This is a two step process, though Privy's Swift SDK wraps this into a single method call:

  1. Generate an OAuth login URL corresponding to your desired OAuth provider
  2. Redirect the user to the login URL to have them authenticate with the chosen OAuth provider

TIP

Prior to integrating OAuth login, make sure you have properly configured your app's allowed URL schemes in the Privy dashboard. Login with OAuth will not work if you have not completed this step.

INFO

Privy's Swift SDK currently only supports OAuth login with Google. We are actively building support for Apple and other OAuth providers as well. If you urgently need support for a given provider, please reach out to support@privy.io

Initializing the login flow

To launch the oAuth flow, simply call privy.oAuth.login. As parameters to this method, pass the following fields:

FieldTypeDescription
providerOAuthProviderA member of the OAuthProvider enum specifying which OAuth provider the user should login with. Currently, only OAuthProvider.google is supported.
appUrlSchemeString(Optional). Your app's URL scheme as a string. If you do not pass this value, Privy will use the first valid app URL scheme from your app's info.plist.

As an example, you might call privy.oAuth.login like so:

swift
do {
    // You can optionally omit the `appUrlScheme` param in this call. If you do, Privy will use the first valid
    // URL scheme from your app's `info.plist`.
    let authSession = try await privy.oAuth.login(with: OAuthProvider.google, appUrlScheme: "privyiosdemo")
} catch {
    debugPrint("Error: \(error)")
    // Handle errors
}

That's it! If your user was successfully authenticated, the login method will return the new AuthSession.

Handling errors

An error could be thrown if:

  • Your app url scheme is not explicitly provided or set in your info.plist
  • Your app url scheme is not registered in the Privy dashboard.
  • There was an issue generating the OAuth provider login URL
  • The user declined or cancelled the login attempt, or there was another error during authentication

If an error is thrown, you can get a description of the error as a string from the error thrown by privy.oAuth.login.