Fixed github callback route redirection, new user is now successfully created on github login
This commit is contained in:
3
app.js
3
app.js
@ -64,8 +64,7 @@ app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }))
|
|||||||
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
|
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' }));
|
||||||
|
|
||||||
app.get('/auth/github', passport.authenticate('github'));
|
app.get('/auth/github', passport.authenticate('github'));
|
||||||
app.get('/auth/github/callback', passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }));
|
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), function(req, res) { res.redirect('/'); });
|
||||||
|
|
||||||
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
|
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
|
||||||
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login' }));
|
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login' }));
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ var passport = require('passport'),
|
|||||||
LocalStrategy = require('passport-local').Strategy,
|
LocalStrategy = require('passport-local').Strategy,
|
||||||
FacebookStrategy = require('passport-facebook').Strategy,
|
FacebookStrategy = require('passport-facebook').Strategy,
|
||||||
TwitterStrategy = require('passport-twitter').Strategy,
|
TwitterStrategy = require('passport-twitter').Strategy,
|
||||||
|
GitHubStrategy = require('passport-github').Strategy,
|
||||||
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
|
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
|
||||||
User = require('../models/User'),
|
User = require('../models/User'),
|
||||||
config = require('./config.json');
|
config = require('./config.json');
|
||||||
@ -65,13 +66,26 @@ passport.use(new FacebookStrategy({
|
|||||||
|
|
||||||
// GITHUB OAUTH2 LOGIN
|
// GITHUB OAUTH2 LOGIN
|
||||||
passport.use(new GitHubStrategy({
|
passport.use(new GitHubStrategy({
|
||||||
clientID: GITHUB_CLIENT_ID,
|
clientID: config.github.clientId,
|
||||||
clientSecret: GITHUB_CLIENT_SECRET,
|
clientSecret: config.github.clientSecret,
|
||||||
callbackURL: "http://127.0.0.1:3000/auth/github/callback"
|
callbackURL: config.github.callbackUrl
|
||||||
},
|
},
|
||||||
function(accessToken, refreshToken, profile, done) {
|
function(accessToken, refreshToken, profile, done) {
|
||||||
User.findOrCreate({ githubId: profile.id }, function (err, user) {
|
User.findOne({ github: profile.id }, function(err, existingUser) {
|
||||||
return done(err, user);
|
if (err) done(err);
|
||||||
|
if (existingUser) return done(null, existingUser);
|
||||||
|
console.log(profile);
|
||||||
|
var user = new User({
|
||||||
|
username: profile.username,
|
||||||
|
displayName: profile.displayName,
|
||||||
|
email: profile.emails[0].value,
|
||||||
|
provider: profile.provider
|
||||||
|
});
|
||||||
|
user[profile.provider] = profile.id;
|
||||||
|
user.save(function(err) {
|
||||||
|
if (err) console.log(err);
|
||||||
|
done(null, user);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
Reference in New Issue
Block a user