【发布时间】:2017-08-12 16:03:22
【问题描述】:
我使用 MEAN 框架开发了一个应用程序,并使用了 passportjs 的 google 策略进行身份验证。本地运行运行良好,但是当我将其部署到 heroku 时,因为 Heroku 在随机端口上运行其应用程序。我不确定我需要在我的谷歌控制台的“授权重定向 URI”中添加什么谷歌回调 url。
passport.use(new GoogleStrategy({
clientID: config.googleAuth.clientID,
clientSecret: config.googleAuth.clientSecret,
callbackURL: config.googleAuth.callbackURL
}, function (token, refreshToken, profile, done) {
console.log(token, refreshToken, profile, done);
var query = {
'google.id' : profile.id
};
User.findOne(query, function (err, user) {
if(user){
console.log("User found in the database");
done(null, user);
}
else{
var newUser = new User;
newUser.displayName = profile.displayName;
newUser.image = profile.photos[0].value;
newUser.google = {};
newUser.google.id = profile.id;
newUser.google.token = token;
newUser.save();
console.log("saved user to the database");
done(null, newUser);
}
});
}));
上面显示的代码是我的谷歌策略。我正在使用 passport-google-oauth 库进行身份验证。
module.exports = {
development: {
rootPath: rootPath,
db: 'xxx',
port: process.env.PORT || 3030,
googleAuth: {
clientID: 'xxx',
clientSecret: 'xxx',
callbackURL: 'http://localhost:3030/auth/google/callback'
}
},
production: {
rootPath: rootPath,
db: 'xxx',
port: process.env.PORT || 80,
googleAuth: {
clientID: 'xxx',
clientSecret: 'xxxx',
callbackURL: 'https://<myheroku-app>:<heroku-port-no>/auth/google/callback'
}
}
}
以上是我的google策略的细节。如果我将 http://localhost:3030/auth/google/callback 添加到授权重定向 URI,则 localhost 部分可以正常工作。但是当我尝试对 heroku 应用程序执行相同操作时,我收到 400 服务器错误,错误为:redirect_uri_mismatch。
我该如何解决这个问题?我非常接近部署这个应用程序并坚持使用这个东西。让我知道您是否需要更多信息。
【问题讨论】:
标签: node.js heroku passport.js mean-stack google-oauth