Last active
October 15, 2018 14:11
-
-
Save workfel/4b363f8da85dba4bd96c21fcf1c8fbb2 to your computer and use it in GitHub Desktop.
nest auth google strategy
This file contains hidden or 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
import { Injectable } from '@nestjs/common'; | |
import { OAuth2Strategy } from 'passport-google-oauth'; | |
import * as passport from 'passport'; | |
@Injectable() | |
export class GoogleStrategy extends OAuth2Strategy { | |
constructor() { | |
// http://www.passportjs.org/docs/google/ | |
super({ | |
clientID: YOUR_GOOGLE_CLIENT_ID, | |
clientSecret: YOUR_GOOGLE_CLIENT_SECRET, | |
callbackURL: 'http://YOUR_SERVER:YOUR_PORT/auth/google/callback', | |
passReqToCallback: true, | |
}, (req, accessToken, refreshToken, profile, done) => { | |
const user: any = { | |
email: profile.emails[0].value, | |
photo: profile.photos[0].value.replace(/sz=50/gi, 'sz=250'), | |
image: profile._json.image.url, | |
displayName: profile.displayName, | |
googleAccount: { | |
googleId: profile.id, | |
googleToken: accessToken, | |
}, | |
}; | |
return done(null, user); | |
}); | |
passport.use(this); | |
passport.serializeUser((user, done) => { | |
done(null, user); | |
}); | |
passport.deserializeUser((user, done) => { | |
done(null, user); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment