Google Authentication
Setting Up Google Authentication for your Application
This tutorial will guide you through the process of configuring Google Authentication in your Expo project, including setting up the OAuth consent screen and generating OAuth 2.0 client IDs for Android, iOS, and web.
Before you begin, ensure you have a Google Cloud account.
Configure the OAuth Consent Screen
-
Create a New Project:
- Go to the Google Cloud Console.
- Click on the project dropdown in the top left corner and select "New Project."
- Give your project a name.
- Click "Create."
-
Set Up the OAuth Consent Screen:
- Go to the OAuth consent screen.
- Choose "External" as the user type
- Click "Create."
-
Fill in the App Information:
- App name: Enter your app's name.
- User support email: Enter a contact email.
- App logo: (Optional) Upload your app's logo.
- App domain: Enter your domain if applicable.
- Authorized domains: Add domains you will use.
- Developer contact information: Enter your email address.
- Click "Save and Continue."
-
Scopes:
- Click "Add or Remove Scopes" and add the required scopes for your app. For basic Google authentication, you’ll need
openid
,email
, andprofile
. - Click "Save and Continue."
- Click "Add or Remove Scopes" and add the required scopes for your app. For basic Google authentication, you’ll need
-
Test Users:
- If your app is in development, you can add test users here. These users will be able to use your app's OAuth functionality even if it's not yet verified.
- Click "Save and Continue."
Create OAuth 2.0 Client IDs
Generate a Client ID for Web
-
Go to Credentials:
- Navigate to the Credentials tab in the Google Cloud Console.
- Click "Create Credentials" > "OAuth 2.0 Client IDs."
-
Configure OAuth Client ID:
- Choose "Web application" as the application type.
- Name your OAuth client (e.g., "Expo Web Client").
-
Set Authorized Redirect URIs:
- Add the following redirect URIs:
- For Expo development:
http://localhost:8081
- For production: Add your website’s domain (e.g.,
https://yourwebsite.com/
).
- For Expo development:
- Click "Create."
- Add the following redirect URIs:
-
Save Client ID and Secret: Once created, save the
Client ID
you’ll need them later.
2. Generate a Client ID for Android
- Create Another OAuth Client ID:
- Click "Create Credentials" > "OAuth 2.0 Client IDs."
- Choose "Android" as the application type.
- Configure OAuth Client ID:
- Name your OAuth client (e.g., "Expo Android Client").
- Add your Android package name (e.g.,
com.yourcompany.yourapp
). - Add your development SHA-1 signing certificate. You can find it by running:
AppStore Signin Keystore
If you use Google Play Signin, you need to use the fingerprint from Google Play Console :
- Click "Create."
- Save Client ID and Secret:
- Save the
Client ID
andClient Secret
.
- Save the
3. Generate a Client ID for iOS
-
Create Another OAuth Client ID:
- Click "Create Credentials" > "OAuth 2.0 Client IDs."
- Choose "iOS" as the application type.
-
Configure OAuth Client ID:
- Name your OAuth client (e.g., "Expo iOS Client").
- Add your iOS bundle identifier (e.g.,
com.yourcompany.yourapp
). - Click "Create."
-
Save Client ID and Secret:
- Save the
Client ID
.
- Save the
Install and Configure the Expo Google Auth Package
Replace iosClientId
, iosUrlScheme
, and webClientId
with the corresponding client IDs from the Google Cloud Console.
Note that the iosUrlScheme
is the reverse of the iOS client ID.
You can download Plist file from Google Console to get the URL scheme to be sure.
Update .env to add Google signin audience to validate the audience of the token on the server side :
It should be the same values as the webClientId
, iosClientId
and androidClientId
separated by a comma.
Test Your Configuration
-
Run Your App:
- For web: Use
pnpm dev
. - For Android: Use
pnpm android
. - For iOS: Use
pnpm ios
.
- For web: Use
-
Sign In:
- Click the "Login with Google" button and follow the sign-in process.
-
Check the Authentication Response:
- Verify that you receive the user's information and authentication token.
Deploy Your App
Once you've successfully tested Google Authentication in your development environment, you can proceed to deploy your app to production. Ensure that you’ve set up the appropriate OAuth consent and client IDs for the production environment.