Integrate Google OAuth via the API

Integrate Google OAuth into your existing login flow.

The first three steps involve setting up a Google Cloud Project to get OAuth Credentials. Skip those steps if you already have your Google OAuth credentials.

Step 1: Set up Google Project

Before you can use the Stytch Google OAuth flow, you need to enter a Google OAuth client ID and client secret into the Stytch dashboard. Google requires you to create a Google Cloud project to get a client ID and client secret.

Please follow the steps here to create one.

Google also requires developers to fill out an OAuth consent form to create OAuth credentials. Google uses this consent form to make sure that an application does not abuse its data.

If you have not filled out the OAuth Consent yet, please visit here to start the process. You should see an form like the following:

OAuth consent start screen

The Google consent page requires you to select whether your application is an external or internal one. Internal applications limit access to accounts within a Google workspace, and you cannot select internal if your Google Cloud project is not a part of a Google Workspace. External applications allow users outside your Google workplace domain to sign up and possibly require you to verify your application with Google. Most use cases pick external over internal.

Once you select internal or external, you need to fill out some information on your company. Under the Authorized domains section, you need to add stytch.com.

OAuth consent authorized domains

After filling out your company's information, you can click save and continue for the scopes and test users pages without any changes.

Once you are on the summary page, go back to Google OAuth Consent Dashboard. You need to publish your application for prod to avoid the rate limit of 100 users.

NOTE: If you added any restrictive scopes or added a logo, your application needs to be manually verified. This verification can take a couple of days to be reviewed. You can read more about Googles OAuth verification here.

Step 3: Set up Google OAuth Credentials

  1. After setting up the Google project and OAuth consent, go to this page to create the OAuth credentials. There is a create credentials button at the top of the page with an option to create an OAuth client id when selected.
  2. When the create OAuth credentials page loads, select Web application (regardless if it is a mobile or non-web application) for the application type and enter a name for your credentials. Under the Authorized redirect URIs section, add the following URL.
    https://test.stytch.com/v1/oauth/callback/REPLACE_WITH_OAUTH_CALLBACK_ID
  3. After you finish creating the OAuth client, Google displays client ID and client secret in a popup. You will need to enter them into the Stytch Dashboard here.

Step 4: Add redirect URLs to the Stytch dashboard

Add the login and signup URLs to the project's list of predefined redirect URLs in the dashboard. For more information on why this step is necessary, please check out the documentation here.

By default, all redirect URLs are set to http://localhost:3000 for the Test environment.

Step 5: Add the Google OAuth start URL

Now that Stytch and Google are configured, you need to add Stytch Google OAuth to your application.

The Stytch OAuth flow is started by having a user click the Google OAuth Start URL.

https://test.stytch.com/v1/public/oauth/google/start?public_token=PUBLIC_TOKEN

This URL redirects the user to the Google OAuth sign-in page with all the required OAuth fields prefilled. Your frontend needs to display Stytch Google OAuth Start URL as a button and send the user to that URL when clicked.

Step 6: Authenticate OAuth request

After Google redirects the user back to Stytch's API, the Stytch API redirects the user to your backend server with a token to either the login redirect URL or the signup redirect URL to verify Stytch redirected the user.

If the response is a 200, the user is verified and can be logged in.

curl --request POST \
  --url https://test.stytch.com/v1/oauth/authenticate \
  -u 'PROJECT_ID:SECRET' \
  -H 'Content-Type: application/json' \
  -d '{
    "token": "SeiGwdj5lKkrEVgcEY3QNJXt6srxS3IK2Nwkar6mXD4="
  }'