Created
March 14, 2022 05:22
-
-
Save doremi31618/e4a879ec458be3c581fe8395fd929e67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const dotenv = require('dotenv'); | |
const passport = require('passport'); | |
const {Strategy} = require('passport-google-oauth20'); | |
const cookieSession = require('cookie-session'); | |
/* to do list */ | |
/* 1. apply a set of google cloud api | |
* 2. init a .env file which include a set of your sensitve datas | |
* 3. app.use(setup_cookie); | |
* 4. app.use(setup_passport); | |
* 5. app.get('/auth/google/callback', passport_authenticate_process); | |
*/ | |
//replcae with your true key | |
const config = { | |
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID, | |
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET , | |
COOKIE_KEY_1: process.env.COOKIE_KEY_1, | |
COOKIE_KEY_2: process.env.COOKIE_KEY_2 | |
} | |
const auth_options = { | |
callbackURL: '/auth/google/callback', | |
clientID: config.GOOGLE_CLIENT_ID, | |
clientSecret: config.GOOGLE_CLIENT_SECRET | |
} | |
const passport_options = { | |
failureRedirect: '/auth/failure', | |
successRedirect: '/auth/success', | |
session: true, | |
failureMessage: true | |
} | |
const cookie_options = { | |
name: 'session', | |
maxAge: 24 * 60 * 60 * 1000, | |
keys: [config.COOKIE_KEY_1, config.COOKIE_KEY_2] | |
} | |
function verifyCallback(accessToken, refreshToken, profile, done){ | |
console.log('google profile', profile); | |
done(null, profile); | |
} | |
passport.use(new Strategy(auth_options, verifyCallback)); | |
passport.serializeUser((user, done)=>{ | |
done(null, user); | |
}); | |
passport.deserializeUser((user, done)=>{ | |
done(null, user); | |
}) | |
module.exports = { | |
setup_cookie: cookieSession(cookie_options), | |
setup_passport: passport.session(), | |
passport_authenticate_process: [ | |
passport.authenticate('google', passport_options), | |
(req, res)=>{console.log('google callback');} | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment