Passer au contenu

Google Connexion Configuration

In this Guide, you will learn how to Configuration Google Connexion with Capgo Social Connexion. You will need the following in order to Configuration Google Connexion:

  • A Google Compte

This step is required regardless of which the platform you decide to use.

In this part, you will Configuration the Connexion screen displayed by Google.

  1. Please go to console.cloud.google.com
  2. Click on the project selector Google Console Project Selector
  3. If you don’t have a project already, please Créer a Nouveau project.
    1. Click on New project New Project button in Google Console
    2. Name your project and click Create Project naming screen showing name field and Create button
    3. Ensure that you are on the right project Project name showing in the selector indicating correct project selection
  4. Start to configure the OAuth consent screen
    1. Click on the Recherche bar

      Google Console search bar
    2. Search for OAuth consent screen and click on it

      Search results showing OAuth consent screen option
    3. Configure the consent screen

      I will assume that you are developing an app open to the public, so I will use the external user type. Please select the user type that suits you the best AND click create

      Click on create

      OAuth consent screen user type selection with External and Internal options
  5. Fill the Information À propos your Application
    1. Let’s start with the App Information

      App Information section showing App name and User support email fields
      • Please type in your App Name

        THIS WILL BE DISPLAYED TO THE USERS

      • Enter the user support email

        You can learn more À propos the Support email here

      1. You CAN Ajouter the Application logo.

        App logo upload section in OAuth consent screen

        This is not obligatory and I will skip this step

      2. You SHOULD configure the App domain

        App domain configuration section with authorized domains field

        I will not do that because this is just a simple demonstration that will NOT get published, but I strongly recommend filling this section.

      3. You HAVE TO provide the developer’s email

        Developer contact information section with email field
      4. Click on save and continue

        Save and Continue button at bottom of form
  6. Configure the scopes
    1. Click on add or remove scopes

      Add or remove scopes button in scopes configuration screen
    2. Select the following scopes and click update

      Scope selection dialog with email and profile scopes selected
    3. Click save and continue

      Save and Continue button in scopes screen
  7. Ajouter a Test Utilisateur
    1. Click on add users Add users button in test users section
    2. Enter your Google email, click enter, and click add Email input field and Add button for test users
    3. Click save and continue Save and Continue button in test users screen
  8. Click back to dashboard Back to dashboard button at bottom of completion page
  9. Soumettre your Application for verification

    I strongly recommend submitting you Application for verification. This is outside the scope of this Tutoriel. You can learn more here. This isn’t required for local Test, but is required for Production.

Differences between online access and offline access

Section titled “Differences between online access and offline access”

There are multiple ways to use Google Connexion with Capacitor. Here is a table that summarizes the differences between the two:

Online accessOffline access
Requires a backend
Long-lived access token
Easy Configuration

Offline mode means that your backend can access Google’s APIs even when the user is not actively using your app (i.e., when the user is “offline” from your app’s perspective). Long-lived access tokens enable this functionality by allowing the backend to call Google APIs on behalf of the user at any time.

Offline mode REQUIRES a backend server. When using offline mode, the frontend receives minimal information (primarily just a server auth code). Without a backend to exchange this code for tokens and user information, offline mode provides no useful data to your frontend application.

If you still do not know which one you should choose, please consider the following scenarios:

  1. You want the Utilisateur to Connexion, immediately after you are going to Problème him a custom JWT. Your Application will NOT call Google APIs

    In this case, choose online access.

  2. Your Application will call some Google APIs from the client, but never from the backend

    In this case, choose online access

  3. Your Application will call some google APIs from the backend, but only when the Utilisateur is actively using the Application

    In this case, choose online access

  4. Your Application will periodically Vérifier the Utilisateur’s calendar, even when he is not actively using the Application

    In this case, choose offline access

In this part of the Tutoriel, I will show how to Valider the Utilisateur on your backend.

This Exemple will be very simple and it will be based on the following technologies:

You can find the code for this Exemple here

As you can see:

VS Code showing Google authentication code that verifies tokens

The idea is rather simple. You send a simple GET request to https://www.googleapis.com/oauth2/v3/tokeninfo and this returns you whether the token is valid or not and if it it is, it gives you the email of the user. It also gives you some other info about the user token

Google OAuth Playground showing token information response with user details

From there, you could Problème the Utilisateur with your own JWT or Problème some sort of session cookie. The possibilities are endless, for the final auth implementation.

If you do want to call Google API’s, I would strongly recommend looking at Google’s OAuth 2.0 Playground. From there you can easily see what APIs you can call.

Using offline access with your own backend

Section titled “Using offline access with your own backend”

In order to use online access you will need the following:

  • An HTTP server

In this Exemple, I will be using the following technologies to provide the offline access in my Application:

The code for this Exemple can be found here

As for the client code, it looks like this:

import { Capacitor } from '@capacitor/core';
import { GoogleLoginOfflineResponse, SocialLogin } from '@capgo/capacitor-social-login';
import { usePopoutStore } from '@/popoutStore'; // <-- specific to my app
const baseURL = "[redacted]";
async function fullLogin() {
await SocialLogin.initialize({
google: {
webClientId: '[redacted]',
iOSClientId: '[redacted]',
iOSServerClientId: 'The same value as webClientId',
mode: 'offline' // <-- important
}
})
const response = await SocialLogin.login({
provider: 'google',
options: {
forceRefreshToken: true // <-- important
}
})
if (response.provider === 'google') {
const result = response.result as GoogleLoginOfflineResponse
const res = await fetch(`${baseURL}/auth/google_offline`, {
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
serverAuthCode: result.serverAuthCode,
platform: Capacitor.getPlatform()
}),
method: "POST"
})
if (res.status !== 200) {
popoutStore.popout("Full google login failed", "check console");
return
}
const { jwt } = await res.json();
const userinfo = await fetch(`${baseURL}/auth/get_google_user`, {
headers: {
Authorization: `Bearer ${jwt}`
}
})
if (userinfo.status !== 200) {
popoutStore.popout("Full google (userinfo) login failed", "check console");
return
}
popoutStore.popout("userinfo res", await userinfo.text());
}
}