【问题标题】:Forward a PassportJS to a form authentication to add email, password etc将 PassportJS 转发到表单身份验证以添加电子邮件、密码等
【发布时间】:2014-11-05 19:01:08
【问题描述】:

所以我关注了这个tutorial。我完成了整个系列,应用程序运行良好。但是,我希望使用社交网络注册的用户,例如。 Facebook 等被重定向到一个单独的表单,他们可以在其中填写他们的姓名电子邮件等。几个问题。

  1. 我是否必须更改数据库架构以允许来自 facebook/twitter 特定内容的电子邮件和名称等的“全局”(这是使用正确的术语吗?)值。像这样:

    var userSchema = mongoose.Schema({
    
    email            : String,
    name             : String
    password         : String,
    
    facebook         : {
        id           : String,
        token        : String,
    },
    
    twitter          : {
        id           : String,
        token        : String,
        displayName  : String,
        username     : String,
    },
    
    google           : {
        id           : String,
        token        : String,
    }
    
    });
    

那么正确的路线是什么?我目前有:

        // TWITTER ROUTES
// route for facebook authentication and login
app.get('/auth/twitter', passport.authenticate('twitter'));

// handle the callback after twitter has authenticated the user
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/signup' }),
function(req, res) {
// The user has authenticated with Twitter.  Now check to see if the profile
// is "complete".  If not, send them down a flow to fill out more details.
if (req.user.isCompleteProfile()) {
  res.redirect('/profile');
} else {
  res.redirect('/complete-profile');
}});

app.get('/complete-profile', function(req, res) {
    res.render('profile-form', { user: req.user });
});

app.post('/update-profile', function(req, res) {
    // Grab the missing info from the form and update the profile.
    res.redirect('/home');
});

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {

// if user is authenticated in the session, carry on
if (req.isAuthenticated ())
    return next();

// if they arent redirect them to the home page
res.redirect('/');
}

我是从这个StackOverflow那里得到的。

如何定义函数isCompleteProfile?目前我有这样的事情:

function isCompleteProfile(req, res, next) {

// if user has completed profile, carry on
if (req.isComplete ())
    return next();

// if they arent redirect to complete-profile
res.redirect('/complete-profile');
}

第一个链接(指向教程)有作者关于如何实现这一点的一些建议,如果有人需要,请在文章下方的评论部分中。不过我主要看的是 SO 链接。

显然它不起作用(我只是复制了isLoggedIn 的早期函数。创建视图应该(希望)不会太困难。因此,如果有人能阐明我应该如何解决这个问题,将不胜感激.

谢谢 乌克兰

PostScript:是否还有潜在的替代方法?我不在路由中使用单独的函数,而是直接将它们重定向到另一个页面?也不确定我会如何做到这一点,因此我们将不胜感激。

【问题讨论】:

    标签: javascript node.js mongodb twitter passport.js


    【解决方案1】:

    请参阅我在 Using Everyauth/Passport.js to authenticate with Twitter whilst asking for username/email/password 上的说明,了解有关如何设置 isCompleteProfile 测试的说明。

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 2019-07-13
      • 1970-01-01
      • 2021-11-21
      • 2018-09-10
      • 2017-05-14
      • 2012-01-13
      • 2016-10-13
      相关资源
      最近更新 更多