Skip to content

attractorai/microauth-google

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

microauth-google

Google oauth for micro

Build Status XO code style Greenkeeper badge

Add google authentication to your micro in few lines of code.

Inspired by Dmitry Pavlovsky microauth-github

Installation

npm install --save microauth-google
# or
yarn add microauth-google

Usage

app.js

const { send } = require('micro');
const microAuthGoogle = require('microauth-google');

const options = {
  clientId: 'CLIENT_ID',
  clientSecret: 'CLIENT_SECRET',
  callbackUrl: 'http://localhost:3000/auth/google/callback',
  path: '/auth/google',
  scope: 'https://www.googleapis.com/auth/plus.me'
};

const googleAuth = microAuthGoogle(options);

// third `auth` argument will provide error or result of authentication
// so it will {err: errorObject} or {result: {
//  provider: 'google',
//  accessToken: 'blahblah',
//  info: userInfo,
//  tokens: {Object with needed tokens to reuse with Google's OAuth2 instance}
// }}
module.exports = googleAuth(async (req, res, auth) => {

  if (!auth) {
    return send(res, 404, 'Not Found');
  }

  if (auth.err) {
    // Error handler
    return send(res, 403, 'Forbidden');
  }

  return `Hello ${auth.result.info.displayName}`;
});

Run:

micro app.js

Now visit http://localhost:3000/auth/google

Author

Rui Pedro Lima

About

Google oauth for micro

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%