gpt4 book ai didi

node.js - Passport : different redirect for login and account registration

转载 作者:IT老高 更新时间:2023-10-28 23:25:17 28 4
gpt4 key购买 nike

我在我的应用程序中使用 Passport 模块(github 身份验证),我想根据操作进行重定向...我检查它是否只是普通登录,或者用户是否是第一次登录。

passport.use(new GitHubStrategy({
clientID: conf.github.app_id,
clientSecret: conf.github.app_secret,
callbackURL: conf.github.callback_url
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {

// To keep the example simple, the user's GitHub profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the GitHub account with a user record in your database,
// and return that user instead.

Models_User.findOrCreateUser(profile, function(msg){
console.log("auth type:" + msg);
});

return done(null, profile);

});
}
));

在我的 findOrCreateUser 函数中,我检查它是否是新用户并执行所有 db 操作......为了测试,我让函数返回一个 msg 变量,它只是一个表示“login”或“new_registration”的字符串。

所以我的问题是如何“传输”我从 findOrCreateUser 获得的变量,以便在 Passport 身份验证完成后我可以相应地重定向(“/welcome”或“/back_again”)。

我的应用程序中的另一个 Passport 代码:

// GET /auth/github
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in GitHub authentication will involve redirecting
// the user to github.com. After authorization, GitHubwill redirect the user
// back to this application at /auth/github/callback
app.get('/auth/github',
passport.authenticate('github'),
//passport.authenticate('github', { scope: ['user', 'public_repo', 'gist'] }),
function(req, res){
// The request will be redirected to GitHub for authentication, so this
// function will not be called.
});

// GET /auth/github/callback
// Use passport.authenticate() as route middleware to authenticate the
// request. If authentication fails, the user will be redirected back to the
// login page. Otherwise, the primary route function function will be called,
// which, in this example, will redirect the user to the home page.
app.get('/auth/github/callback',
passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});

最佳答案

在您的验证回调中,我会进行更改,以便 findOrCreateUser 函数将实际记录提供给回调,然后将其传递给 done(),像这样:

Models_User.findOrCreateUser(profile, function(user){
console.log("auth type:" + msg);
return done(null, user);
});

// take this out, use the actual model above
//return done(null, profile);

现在,在验证后处理回调 URL 时,您可以检查此用户记录,看看它是否是新的(我假设它在这里有一个 isNew 属性):

app.get('/auth/github/callback', 
passport.authenticate('github', { failureRedirect: '/login' }),
function(req, res) {
// successful auth, user is set at req.user. redirect as necessary.
if (req.user.isNew) { return res.redirect('/back_again'); }
res.redirect('/welcome');
});

关于node.js - Passport : different redirect for login and account registration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10111445/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com