Google oauth for micro
Add google authentication to your micro in few lines of code.
Inspired by Dmitry Pavlovsky microauth-github
npm install --save microauth-google
# or
yarn add microauth-google
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