Passer au contenu

Google Connexion on Web

In this Guide, you will learn how to Configuration Google Connexion with Capgo Social Connexion for web applications. I assume that you have already read the general Configuration Guide.

Using the google Connexion on the web is rather simple. In order to use it, you have to do the following:

  1. Créer a web client in the Google Console

    If you have already configured Google Connexion for Android, you can skip this step as you’ve already created a web client. You can proceed directly to step 2.

    1. Click on the Recherche bar

      Google Console search bar
    2. Search for credentials and click on the APIs and Services option (number 2 on the screenshot)

      Search results showing credentials option with APIs and Services highlighted
    3. Click on the create credentials

      Create credentials button in Google Console
    4. Select OAuth client ID

      OAuth client ID option in credentials creation menu
    5. Select the Application type as Web application

      Application type selection with Web option highlighted
    6. Name your client and click Create

      Web client creation form with name field highlighted
    7. Copy the client ID, you’ll use this as the webClientId in your application

      Client ID details showing Web client ID to copy
  2. Configure the web client in the Google Console

    1. Please open the credentials page and click on your web client

      Credentials list showing web client to click
    2. Now, please add the Authorized JavaScript origins. This should include all the addresses that you might use for your app. In might case, I will ONLY use localhost, but since I use a custom port I have to add both http://localhost and http://localhost:5173

      1. Please click on add URI

        Authorized JavaScript origins section with ADD URI button
      2. Please type your URL

        ADD URI dialog with localhost URL entered
      3. Please repeat until you added all the URLs

      4. When you finish, your screen should look something like this

        Authorized JavaScript origins with multiple localhost URLs added
    3. Now, please add some Authorized redirect URIs. This will depend on what page do you depend to use the CapacitorSocialLogin plugin on. In my case, I am going to be using it on http://localhost:5173/auth

      1. Please click on ADD URI

        Authorized redirect URIs section with ADD URI button
      2. Enter your URL and click ADD URL again

        ADD URI dialog with redirect URL entered
    4. Click save

      Save button at bottom of web client configuration
  3. Now, you should be ready to call login from JavaScript like so:

    1. First, import SocialLogin

      import { SocialLogin } from '@capgo/capacitor-social-login';
    2. Then, call Initialiser. This should be called ONLY once.

      // onMounted is Vue specific
      // webClientId is the client ID you got in the web client creation step not the android client ID.
      onMounted(() => {
      SocialLogin.initialize({
      google: {
      webClientId: '673324426943-avl4v9ubdas7a0u7igf7in03pdj1dkmg.apps.googleusercontent.com',
      }
      })
      })

    Web Redirect Handling

    When using Google login on web, you MUST call any function from the plugin when the redirect happens to initialize the plugin so it can handle the redirect and close the popup window. You can call either isLoggedIn() OR initialize() - both will trigger the redirect handling:

    // Option 1: Call isLoggedIn when the redirect page loads
    SocialLogin.isLoggedIn({ provider: 'google' }).catch(() => {
    // Ignore the result, this is just to initialize the plugin
    });
    // Option 2: Call initialize when the redirect page loads
    SocialLogin.initialize({
    google: {
    webClientId: 'YOUR_WEB_CLIENT_ID.apps.googleusercontent.com',
    }
    }).catch(() => {
    // Ignore any errors, this is just to handle the redirect
    });
    1. Create a login button that calls SocialLogin.login when clicked

      const res = await SocialLogin.login({
      provider: 'google',
      options: {}
      })
      // Handle the response
      console.log(JSON.stringify(res));