2021-06-12 07:35:10 +08:00
|
|
|
import {Router} from "express";
|
|
|
|
import * as passport from "passport";
|
|
|
|
import {Strategy as GoogleStrategy} from "passport-google-oauth20";
|
2017-04-12 05:41:14 +08:00
|
|
|
|
2021-06-12 07:35:10 +08:00
|
|
|
import * as config from "../../config";
|
|
|
|
import {passportGeneralCallback, setReturnToFromReferer} from "../utils";
|
2017-04-12 05:41:14 +08:00
|
|
|
|
2019-08-02 00:59:10 +08:00
|
|
|
const googleAuth = module.exports = Router()
|
2017-04-12 05:41:14 +08:00
|
|
|
|
|
|
|
passport.use(new GoogleStrategy({
|
|
|
|
clientID: config.google.clientID,
|
|
|
|
clientSecret: config.google.clientSecret,
|
2019-03-09 14:42:06 +01:00
|
|
|
callbackURL: config.serverURL + '/auth/google/callback',
|
2019-04-12 17:58:06 +08:00
|
|
|
userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo'
|
2017-04-12 05:41:14 +08:00
|
|
|
}, passportGeneralCallback))
|
|
|
|
|
2017-09-04 16:04:20 +08:00
|
|
|
googleAuth.get('/auth/google', function (req, res, next) {
|
2017-04-12 05:41:14 +08:00
|
|
|
setReturnToFromReferer(req)
|
2020-02-28 17:17:10 +08:00
|
|
|
passport.authenticate('google', {
|
|
|
|
scope: ['profile'],
|
|
|
|
hostedDomain: config.google.hostedDomain
|
|
|
|
})(req, res, next)
|
2017-04-12 05:41:14 +08:00
|
|
|
})
|
2019-04-12 17:58:06 +08:00
|
|
|
// google auth callback
|
2017-09-04 16:04:20 +08:00
|
|
|
googleAuth.get('/auth/google/callback',
|
2017-04-12 05:41:14 +08:00
|
|
|
passport.authenticate('google', {
|
2018-03-07 15:17:35 +01:00
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/'
|
2017-04-12 05:41:14 +08:00
|
|
|
})
|
|
|
|
)
|