@attractor/microauth-google

0.0.5 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i @attractor/microauth-google

Weekly Downloads

1

Version

0.0.5

License

MIT

Unpacked Size

7.37 kB

Total Files

6

Last publish

Collaborators

  • inlife