Google Plus身份验证回拨无效
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Google Plus身份验证回拨无效相关的知识,希望对你有一定的参考价值。
我添加了Google +登录,但回调似乎不起作用。
注意:代码改编自本教程:https://scotch.io/tutorials/easy-node-authentication-google
情景
- 我转到URL:https://gym-up-server.herokuapp.com/api/v1/oauth/google
- 这将通过其网站启动Google登录流程
- 登录成功+数据插入mysql DB
- 应用程序然后被带到错误页面
它似乎未能调用回调URL,该URL应该是:https://gym-up-server.herokuapp.com/api/v1/oauth/google/callback
我正在使用以下依赖项:Express,MySQL,Sequelize,Passport
这是我的代码:
路线:
// =====================================
// GOOGLE ROUTES =======================
// =====================================
// send to google to do the authentication
// profile gets us their basic information including their name
// email gets their emails
router.get(
"/google",
passport.authenticate("google", { scope: ["profile", "email"] })
);
// the callback after google has authenticated the user
router.get("/google/callback", passport.authenticate("google"),
function(req,res) {
res.render("index");
console.log("After Passport AUTH");
});
护照JS
// =========================================================================
// GOOGLE ==================================================================
// =========================================================================
passport.use(
new GoogleStrategy(
{
clientID: configAuth.googleAuth.clientID,
clientSecret: configAuth.googleAuth.clientSecret,
callbackURL: configAuth.googleAuth.callbackURL
},
function(token, refreshToken, profile, done) {
console.log("getting data from Google");
// make the code asynchronous
// User.findOne won't fire until we have all our data back from Google
process.nextTick(function() {
// try to find the user based on their google id
console.log("profile ID :", profile.id);
models.User.findOne({ where: { ggId: profile.id } }).then(function(
user
) {
// if (err) return done(err);
if (user) {
// if a user is found, log them in
return done(null, user);
} else {
// if the user isnt in our database, create a new user
var data = {
// set all of the relevant information
ggId: profile.id,
ggToken: token,
ggName: profile.displayName,
ggEmail: profile.emails[0].value // pull the first email
};
//save user
models.User.create(data, {
fields: ["ggId", "ggToken", "ggName", "ggEmail"]
}).then(function(insertedUser) {
console.log(
"User Created!" + ": " + insertedUser.get({ plain: true })
);
// console.log("about to run DONE to go back to ROUTE");
// return done(null, insertedUser.get({ plain: true }));
if (!insertedUser) {
console.log("failed to insert user - nothing found!");
return done(null, false);
}
if (insertedUser) {
console.log("about to run DONE to go back to ROUTE");
return done(null, insertedUser);
}
});
}
});
});
}
)
);
答案
您需要将回调网址传递给回调路由中的Google身份验证。您提到的教程有successRedirect和failureRedirect路由。
例如:添加成功和失败路由如下。
// route for success
app.get('/', function(req, res) {
res.render('index.ejs'); // load the index.ejs file
});
// route for failure
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
然后将通过成功和失败路由添加到谷歌身份验证
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect : '/profile',
failureRedirect : '/'
}));
另一答案
我找到了解决方案。但我不完全确定它为什么会起作用。
// the callback after google has authenticated the user
router.get("/google/callback", function(req, res, next) {
passport.authenticate("google", function(err, user, info) {
res.redirect("/profile");
})(req, res, next);
});
希望这有助于其他人。花了很长时间才弄明白这个!
以上是关于Google Plus身份验证回拨无效的主要内容,如果未能解决你的问题,请参考以下文章
Cordova Android 应用 Google Plus 身份验证错误 10
使用 Google Plus 进行身份验证,使用 android App,如何获取 REFRESH TOKEN
通过 Google 打开 auth 2.0 身份验证后重定向 url 无效