javascript - 将 PassportJS 转发到表单例份验证以添加电子邮件、密码等

标签 javascript node.js mongodb twitter passport.js

所以我遵循了这个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 的早期函数。创建 View 应该(希望)不会太困难。因此,如果有人能够阐明我应该如何解决这个问题,我将不胜感激。

谢谢 英国

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

最佳答案

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

关于javascript - 将 PassportJS 转发到表单例份验证以添加电子邮件、密码等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25794051/

相关文章:

node.js - 在 NodeJS 中将缓冲区数据转换为 blob

javascript - 如何比较嵌套数组中的元素

javascript - 如何使用 JavaScript 验证图像大小(表示为例如 “5X4” )?

javascript - JS 图像 slider /旋转木马显示 5 个元素而不是 1 个

javascript - 如何将此 node.js 模块降级到特定版本并防止以后自动升级?

node.js - Web3 + 坚固性 : Passing in arguments to a contract's constructor

javascript - 如何在 ReactJS 中触发模型更改的重新渲染?

mongodb - 如何将 mongo 命令生成为平面文件

MongoDB:查找字段名称以开头的对象

mongodb - 通过ansible playbook启用mongo身份验证