0.0.1 • Published 4 years ago

microauth-google-fork v0.0.1

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

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