mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-29 08:07:48 +01:00
auth using google
This commit is contained in:
@@ -10,6 +10,12 @@ const production = {
|
||||
process.env.MONGODB_URI ||
|
||||
'mongodb://localhost/todolist',
|
||||
},
|
||||
googleOAuth: {
|
||||
googleEnabled: process.env.GOOGLE_ENABLED,
|
||||
googleClientId: process.env.GOOGLE_CLIENT_ID,
|
||||
googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
||||
googleCallback: `${process.env.HOST}/api/users/login/google/callback`,
|
||||
},
|
||||
secret: process.env.SECRET,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,31 @@
|
||||
const passport = require('passport');
|
||||
const mongoose = require('mongoose');
|
||||
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
|
||||
const {
|
||||
googleClientId,
|
||||
googleClientSecret,
|
||||
googleCallback,
|
||||
googleEnabled,
|
||||
} = require('./').googleOAuth;
|
||||
|
||||
const User = mongoose.model('User');
|
||||
|
||||
passport.use(User.createStrategy());
|
||||
|
||||
if (googleEnabled) {
|
||||
passport.use(
|
||||
new GoogleStrategy(
|
||||
{
|
||||
clientID: googleClientId,
|
||||
clientSecret: googleClientSecret,
|
||||
callbackURL: googleCallback,
|
||||
},
|
||||
(accessToken, refreshToken, profile, done) => {
|
||||
User.findOrCreate({ googleId: profile.id }, (err, user) =>
|
||||
done(err, user),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
module.exports = passport;
|
||||
|
||||
Reference in New Issue
Block a user