【发布时间】:2021-04-27 11:46:15
【问题描述】:
我在本地测试过这个passport google策略,效果很好,回调函数调用成功,可以使用google账号登录。但是,当我将此代码推送到生产服务器时,我看到回调函数没有被调用。从用户的角度来看,当他们单击链接以使用 google 策略时,他们会看到 google 的“选择帐户”屏幕,然后加载一段时间,然后再将它们转回到他们之前所在的页面上。我看不出问题出在哪里。提前感谢您的帮助!
passport.use('google', new GoogleStrategy({
clientID: "redacted",
clientSecret: "redacted",
callbackURL: "/google/callback"
},
function(accessToken, refreshToken, profile, done) {
const userQueryString = `
SELECT firstName, lastName, AgentReference, agent_number, website_password, account_type, status, state, BIN(Flags) as flags
FROM compass.agent
WHERE googleID = ?`;
console.log("google profile: \n", profile);
//pull the user data for the agent with the same googleID as the selected accoutn at login.
db.query(userQueryString,[profile.id],(err, response, fields)=>{
if(err) {
done(err);
} else if(response.length === 0) {
done(null, false);
} else if(response[0].status === 'Terminated') {
console.log('account is terminated.');
done(null, false);
} else {
let user = {
id: response[0].AgentReference
, accountType: response[0].account_type
, agent_number:response[0].agent_number
, name:response[0].firstName + ' ' + response[0].lastName
, state: response[0].state
, flags: response[0].flags
};
let flagRegex = RegExp('^[0-1]*1[0-1]{9}$');
if(flagRegex.test(user.flags)){
console.log(JSON.stringify(user));
return done(null, user);
} else {
return done(null, false, {message: "You do not have access to this feature. Please speak with your manager for more information."})
}
}
});
}
)
);
【问题讨论】:
标签: node.js oauth-2.0 google-oauth passport.js