Google OAuth Setup
Configure Google OAuth to enable social login for your ZapStart application
Introduction
Google OAuth allows users to sign up and log in to your application using their Google accounts. This provides a seamless authentication experience without requiring users to create and remember another set of credentials.
Social login increases conversion rates by making it easier for users to create accounts and access your application.
Google Cloud Setup
To use Google OAuth, you'll need to create a project in the Google Cloud Console:
- Go to the Google Cloud Console
- Click on APIs & Services
- Click on Create project
- Enter a name for your project, e.g. ZapStart
- Click on Create
- Fill in the required fields like App name and User support email
- Choose External for audience then continue.
- One the overview page click on Create OAuth client or go directly to Client from the left menu and then click on Create client
Create OAuth Client
- Select Web application as the application type
- Set a name for your OAuth client like ZapStart
- Under Authorized JavaScript origins, add:
http://localhost:3001
and add the your production domainhttps://yourdomain.com
- Under Authorized redirect URIs, add:
http://localhost:3000/api/auth/callback/google
and add the your production callback URLhttps://api.yourdomain.com/api/auth/callback/google
Later we will make the api prefix of our domain points to the backend server. - Click Create
- Copy the Client ID and Client Secret to your .env file into these two variables:
GOOGLE_CLIENT_ID=your_client_id_here
GOOGLE_CLIENT_SECRET=your_client_secret_here
GOOGLE_REDIRECT_URI=http://localhost:3000/api/auth/callback/google
Note for the GOOGLE_REDIRECT_URI we added the local uri, but in production it should be the callback uri of your backend server.
Configure Data Access
In the Google Cloud Console, you'll need to configure what data your application can access:
- Navigate to APIs & Services -> OAuth consent screen
- Under Scopes, click Add or Remove Scopes
- Select the necessary scopes for your application:
openid
- For OpenID Connect authenticationhttps://www.googleapis.com/auth/userinfo.email
- For email accesshttps://www.googleapis.com/auth/userinfo.profile
- For profile information
Only request the scopes your application actually needs. Requesting unnecessary scopes may make users hesitant to grant access to your application.
At this point you should be able to use the google oauth in your app but under the test mode.
Configure Branding
Customize how your application appears to users during the OAuth flow:
- In the OAuth consent screen, go to the Branding section from the left menu
- Fill in the required fields like App name and User support email
- For privacy policy and terms of service, go to the tos/ and privacy-policy/ pages in your frontend app and create those pages using the instructions in the files, even though the app is not yet launched but you will need to enter the urls in the google console,
https://yourdomain.com/tos
andhttps://yourdomain.com/privacy-policy
While some branding elements are optional, having a complete and professional branding setup helps build trust with your users during the authentication process.
Domain Verification
Before you can publish your OAuth app to production, Google requires that you verify ownership of your domain:
- Go to Google Search Console
- Select the URL prefix property type and enter your domain (e.g.,
https://yourdomain.com
) - Follow the verification steps, which typically involve adding a DNS record to your domain's DNS configuration
- Once verified, you can reference this domain in your Google Cloud OAuth configuration
Domain verification is a critical step for publishing your OAuth application. Without it, Google may limit your application's functionality or require additional verification steps.
Moving to Production
When you're ready to deploy your application to production:
- Ensure you've completed the domain verification process
- Return to the Google Cloud Console's OAuth consent screen
- Click Publish App to move from testing to production, you will probably always find the Publish App button in the audience section
- Add your production URLs to the OAuth client credentials:
- Add your production domain (e.g.,
https://yourdomain.com
) to Authorized JavaScript origins - Add your production callback URL (e.g.,
https://api.yourdomain.com/api/auth/callback/google
) to Authorized redirect URIs if you didn't add them already
- Add your production domain (e.g.,
- Update your production environment variables with the correct redirect URI:
GOOGLE_REDIRECT_URI=https://api.yourdomain.com/api/auth/callback/google
The verification process can be time-consuming, especially if your app uses sensitive scopes. Plan accordingly and start the verification process well before your intended launch date because it takes few days and sometimes weeks.
Troubleshooting
Common issues and their solutions:
Error: redirect_uri_mismatch
The redirect URI in your request doesn't match the ones authorized for your OAuth client.
Solution:
- Ensure the GOOGLE_REDIRECT_URI in your .env exactly matches what you added in the Google console
- Check for typos and HTTP vs. HTTPS inconsistencies
Error: invalid_client
The OAuth client was not found or is invalid.
Solution:
- Double-check your GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET values
- Ensure your OAuth client is properly created and enabled
Error: Test users only
A non-test user tried to log in while your app is in Testing mode.
Solution:
- Add the user to your test users list, or
- Publish your app to production if you're ready
For a tutorial on User Authentication and Authorization, please see the User Authentication tutorial.
Next Steps
Now that you've configured your JWT secrets, MongoDB, and Google OAuth, you can continue setting up the rest:
- Set up email services
- Integrate payment processing
- Fill in the Config files with the correct values