# Google

To enable Google login, add the app id, app secret and scope property to config/default.json:

{
  "authentication": {
    "oauth": {
      "google": {
        "key": "<App ID>",
        "secret": "<App Secret>",
        "scope": ["openid"]
      }
    }
  }
}

According to the documentation of Google (opens new window): "The scope value must begin with the string openid and then include profile or email or both.".

To also request the email address, add the string "email" to the array of the 'scope' property:

{
  "authentication": {
    "oauth": {
      "google": {
        "key": "<App ID>",
        "secret": "<App Secret>",
        "scope": ["openid", "email"],
        "nonce": true
      }
    }
  }
}

The property 'nonce', according to the documentation: "A random value generated by your app that enables replay protection.".

# Application client and secret

The client id (App ID) and secret can be acquired by creating a OAuth client ID (opens new window):

  1. Click on 'OAuth client ID' Creating OAuth client ID - step 1
  2. Select 'web application', fill in the information and click 'Create' Creating OAuth client ID - step 2

Important: Fill in the callback url, in a default Feathers setup it will be /oauth/google/callback.

  1. Replace <App ID> and <App Secret> with the id and secret of the created OAuth client ID application Creating OAuth client ID - step 3
{
  "authentication": {
    "oauth": {
      "google": {
        "key": "481298021138-hv27glb811ocr7pdon5lsg8hh5a6pgjv.apps.googleusercontent.com",
        "secret": "XkWl0witdP4ogeNIgyOi-CeS",
        "scope": ["openid", "email"],
        "nonce": true
      }
    }
  }
}

Note: Use the generated credentials of the OAuth client ID, the key and secret in the example no longer exist.

# Using the data returned from the Google App through a custom OAuth Strategy

In src/authentication.js:

const axios = require('axios');
const { OAuthStrategy } = require('@feathersjs/authentication-oauth');

class GoogleStrategy extends OAuthStrategy {
  async getEntityData(profile) {
  
    // this will set 'googleId'
    const baseData = await super.getEntityData(profile);
    
    // this will grab the picture and email address of the Google profile
    return {
      ...baseData,
      profilePicture: profile.picture,
      email: profile.email
    };
  }
}

module.exports = app => {
  const authentication = new AuthenticationService(app);

  authentication.register('jwt', new JWTStrategy());
  authentication.register('local', new LocalStrategy());
  authentication.register('google', new GoogleStrategy());

  app.use('/authentication', authentication);
  app.configure(expressOauth());
};

Important: googleId, profilePicture and email are properties that should exist on the database model!

Anything unclear or missing? Get help (opens new window) or Edit this page (opens new window)

Last Updated: 11/17/2020, 3:17:03 PM