Home

Awesome

hapi auth google <img width="300" alt="login with google" src="https://developers.google.com/accounts/images/sign-in-with-google.png">

Let people authenticate with your application/website using their Google Account.

Build Status codecov.io Code Climate Dependency Status devDependency Status

Why?

As of May 2017, Google has over 2 Billion Android users alone. In addition, there are over a billion who use gmail (thus, have a Google account) as of 2016, and THEN there are all the Google For Education users, which was 45 million in 2015 and was growing at a rate of 40+%/year. To put it conservatively, there are more than 2 Billion users out there who could authenticate with your app using that Google account so offering people the option of logging into your App(s) using their Google Account makes a lot of sense.

What?

This plugin lets you easily integrate Google Authentication into a Hapi-based Web Application / API.

Key Advantages of This Plugin:

OAuth2 workflow

How? (Usage)

1. Install hapi-auth-google from NPM

Install the plugin from npm and save it to your package.json:

npm install hapi-auth-google --save

2. Create an App on the Google Developer Console

To get access to the Google Account (Plus) API you will first need to create an app
by visiting the google developer console: https://console.developers.google.com

If you are totally new to using the Google API, we created GOOGLE-APP-STEP-BY-STEP-GUIDE just for you!
( Note: if you still have any questions, ask! )

3. Export the Required Environment Variables

Once you've created your app following the GOOGLE-APP-STEP-BY-STEP-GUIDE

Export the Environment Variables:

GOOGLE_CLIENT_ID=YourAppsClientId.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=SuperSecret
PORT=8000
BASE_URL=http://localhost:8000 # Must be identical to "Authorized JavaScript Origin"
JWT_SECRET=SomethingSuperHardToGuess-->grc.com/passwords.htm # Optionally use JWTs

We export the two variables prefixed with GOOGLE_ to distinguish them from other services you may be using.

The BASE_URL is required to know which url your app is using. it needs to be identical to the Authorized JavaScript Origin that you set in step 2.8 above.

Note: If you (or anyone on your team) are new to Environment Variables or need a refresher,
see: https://github.com/dwyl/learn-environment-variables

4. Create Your (Custom) Handler Function

This is where you decide what to do with the person's profile details
once they have authorized your App to use Google details.

Your custom handler should have the following signature:

function custom_handler(request, reply, tokens, profile) {
  // save the profile as a session so you can personalize their experience of your app
  // use the reply() to send a response/view to the visitor
}

The handler function parameters are:

If you get stuck check out: /example/google_oauth_handler.js

5. Register the Plugin into your Hapi.js Server

The final step is to register the plugin into your Hapi.js Server declaring your desired options:

// declare your desired options for the plugin
var opts = {
  REDIRECT_URL: '/googleauth', // must match google app redirect URI from step 2.8
  handler: require('./google_oauth_handler.js'), // your handler
  config: {  // optional route config (as for any route in hapi)
    description: 'Google auth callback',
    notes: 'Handled by hapi-auth-google plugin',
    tags: ['api', 'auth', 'plugin']
  },
  access_type: 'online', // options: offline, online
  approval_prompt: 'auto', // options: always, auto
  scope: 'https://www.googleapis.com/auth/plus.profile.emails.read', // ask for their email address
  // can use process.env or if you prefer, define here in options:
  BASE_URL: process.env.BASE_URL,
  GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
  GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET
};

server.register([{ register: require('hapi-auth-google'), options:opts }],
 function (err) {
  if(err){
    // handle the error if the plugin failed to load:  
  }
  // the rest of your app ...
});

options explained

Need an Example ?

See: /example directory in this repo for a quick example.

Dependencies

This plugin depends on the Official google-api-nodejs-client - to do the authentication with Google and access to other Google Services. Build Status Coverage Status Dependency Status

Background Reading

If you are new to OAuth2, see: