Passportjs Google 身份验证失败
Passportjs Google Authentication Failing
我正在尝试调试 Google OAuth2 设置中甚至无法呈现 OAuth 登录 window 以使用我的凭据登录的地方。尽管遵循了文档,但在触发路由时调用 passport.authorize('google', { scope : ['profile'] })
时,我的代码似乎正在失败,然后出现无限循环,直到超时并出现错误 GET http://localhost:3000/auth/google net::ERR_CONNECTION_REFUSED
.
我试图查看我的护照设置,但文档中的代码示例之间似乎没有什么不同,并且不确定错误的来源,因为我的终端中没有记录任何内容用于我的控制台日志之外Google Auth triggered
这是 PassportJS 设置:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var GoogleStrategy = require('passport-google-oauth20').Strategy;
var models = require('../app/models/db-index');
var configAuth = require('./auth');
var bcrypt = require('bcrypt-nodejs');
/*==== Passport Configuration ====*/
// Serialize sessions
passport.serializeUser(function(user, done) {
console.log("User ID: " + user.userId + " is serializing");
done(null, user.userId);
});
passport.deserializeUser(function(userId, done) {
models.User.find({
where: {
userId: userId
}
}).then(function(user) {
console.log("User ID: " + user.userId + " is deserializing");
done(null, user);
}).error(function(err) {
done(err, null);
});
});
/*==== Local Configuration ====*/
//Login logic
passport.use('local', new LocalStrategy({
passReqToCallback: true,
usernameField: 'email'
}, function(req, email, password, done) {
//Find user by email
models.User.findOne({
where: {
email: req.body.email
}
}).then(function(user) {
if (!user) {
done(null, false, {
message: 'The email you entered is incorrect'
}, console.log("Unknown User"));
} else if (!user.validPassword(password)) {
done(null, false, console.log("Incorrect Password"));
} else {
console.log("User match");
done(null, user);
}
}).catch(function(err) {
console.log("Server Error");
return done(null, false);
});
}));
//Sign Up Logic
passport.use('local-signup', new LocalStrategy({
passReqToCallback: true,
usernameField: 'email'
}, function(req, email, password, done) {
models.User.findOne({
where: {
email: email
}
}).then(function(existingUser) {
if (existingUser)
return done(null, false, req.flash('error', 'Email already exists.'));
if (req.user && password === confirmPassword) {
var user = req.user;
user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.password = models.User.generateHash(password);
user.save().catch(function(err) {
throw err;
}).then(function() {
done(null, user, req.flash('error', 'All fields need to be filled in'));
});
} else {
var newUser = models.User.build({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: models.User.generateHash(password)
});
newUser.save().then(function() {
done(null, newUser);
}).catch(function(err) {
done(null, false, console.log(err));
});
}
}).catch(function(e) {
done(null, false, req.flash('error', 'All fields need to be filled in'), console.log(e.email + e.message));
})
}));
/*==== Google Configuration ====*/
passport.use(new GoogleStrategy({
clientID: 'client-id-hash.apps.googleusercontent.com',
clientSecret: 'secret-hash',
callbackURL: 'http://localhost:3000/auth/google/callback'
}, function(accessToken, refreshToken, profile, cb) {
console.log('First Google Config fired');
models.ExternalAccount.findOrCreate({
externalSourceId: profile.id
}).then(function(err, externalAccount){
console.log('Sequelize Google query fired without issues');
return cb(err, externalAccount)
});
}));
module.exports = passport;
这是我的路线:
var express = require('express');
var siteRoutes = express.Router();
var path = require('path');
var passport = require(path.resolve(__dirname, '..', '..', './config/passport.js'));
/*==== /AUTH/GOOGLE ====*/
siteRoutes.route('/auth/google')
.get(function(req, res){
console.log('Google Auth triggered');
passport.authorize('google', { scope : ['profile'] })
});
/*==== /AUTH/GOOGLE/CALLBACK ====*/
siteRoutes.route('/auth/google/callback')
.get(function(req, res){
passport.authorize('google', {
successRedirect : '/app',
failureRedirect : '/login',
failureFlash: 'Invalid Google credentials.'
});
});
身份验证调用本身就是一个中间件。据我从文档中可以看出,根本没有 authorize
调用。因此,您的回调路线应如下所示:
/*==== /AUTH/GOOGLE/CALLBACK ====*/
siteRoutes.route('/auth/google/callback')
.get(passport.authenticate('google', {
successRedirect : '/app',
failureRedirect : '/login',
failureFlash: 'Invalid Google credentials.'
}));
我正在尝试调试 Google OAuth2 设置中甚至无法呈现 OAuth 登录 window 以使用我的凭据登录的地方。尽管遵循了文档,但在触发路由时调用 passport.authorize('google', { scope : ['profile'] })
时,我的代码似乎正在失败,然后出现无限循环,直到超时并出现错误 GET http://localhost:3000/auth/google net::ERR_CONNECTION_REFUSED
.
我试图查看我的护照设置,但文档中的代码示例之间似乎没有什么不同,并且不确定错误的来源,因为我的终端中没有记录任何内容用于我的控制台日志之外Google Auth triggered
这是 PassportJS 设置:
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var GoogleStrategy = require('passport-google-oauth20').Strategy;
var models = require('../app/models/db-index');
var configAuth = require('./auth');
var bcrypt = require('bcrypt-nodejs');
/*==== Passport Configuration ====*/
// Serialize sessions
passport.serializeUser(function(user, done) {
console.log("User ID: " + user.userId + " is serializing");
done(null, user.userId);
});
passport.deserializeUser(function(userId, done) {
models.User.find({
where: {
userId: userId
}
}).then(function(user) {
console.log("User ID: " + user.userId + " is deserializing");
done(null, user);
}).error(function(err) {
done(err, null);
});
});
/*==== Local Configuration ====*/
//Login logic
passport.use('local', new LocalStrategy({
passReqToCallback: true,
usernameField: 'email'
}, function(req, email, password, done) {
//Find user by email
models.User.findOne({
where: {
email: req.body.email
}
}).then(function(user) {
if (!user) {
done(null, false, {
message: 'The email you entered is incorrect'
}, console.log("Unknown User"));
} else if (!user.validPassword(password)) {
done(null, false, console.log("Incorrect Password"));
} else {
console.log("User match");
done(null, user);
}
}).catch(function(err) {
console.log("Server Error");
return done(null, false);
});
}));
//Sign Up Logic
passport.use('local-signup', new LocalStrategy({
passReqToCallback: true,
usernameField: 'email'
}, function(req, email, password, done) {
models.User.findOne({
where: {
email: email
}
}).then(function(existingUser) {
if (existingUser)
return done(null, false, req.flash('error', 'Email already exists.'));
if (req.user && password === confirmPassword) {
var user = req.user;
user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.password = models.User.generateHash(password);
user.save().catch(function(err) {
throw err;
}).then(function() {
done(null, user, req.flash('error', 'All fields need to be filled in'));
});
} else {
var newUser = models.User.build({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: models.User.generateHash(password)
});
newUser.save().then(function() {
done(null, newUser);
}).catch(function(err) {
done(null, false, console.log(err));
});
}
}).catch(function(e) {
done(null, false, req.flash('error', 'All fields need to be filled in'), console.log(e.email + e.message));
})
}));
/*==== Google Configuration ====*/
passport.use(new GoogleStrategy({
clientID: 'client-id-hash.apps.googleusercontent.com',
clientSecret: 'secret-hash',
callbackURL: 'http://localhost:3000/auth/google/callback'
}, function(accessToken, refreshToken, profile, cb) {
console.log('First Google Config fired');
models.ExternalAccount.findOrCreate({
externalSourceId: profile.id
}).then(function(err, externalAccount){
console.log('Sequelize Google query fired without issues');
return cb(err, externalAccount)
});
}));
module.exports = passport;
这是我的路线:
var express = require('express');
var siteRoutes = express.Router();
var path = require('path');
var passport = require(path.resolve(__dirname, '..', '..', './config/passport.js'));
/*==== /AUTH/GOOGLE ====*/
siteRoutes.route('/auth/google')
.get(function(req, res){
console.log('Google Auth triggered');
passport.authorize('google', { scope : ['profile'] })
});
/*==== /AUTH/GOOGLE/CALLBACK ====*/
siteRoutes.route('/auth/google/callback')
.get(function(req, res){
passport.authorize('google', {
successRedirect : '/app',
failureRedirect : '/login',
failureFlash: 'Invalid Google credentials.'
});
});
身份验证调用本身就是一个中间件。据我从文档中可以看出,根本没有 authorize
调用。因此,您的回调路线应如下所示:
/*==== /AUTH/GOOGLE/CALLBACK ====*/
siteRoutes.route('/auth/google/callback')
.get(passport.authenticate('google', {
successRedirect : '/app',
failureRedirect : '/login',
failureFlash: 'Invalid Google credentials.'
}));