node.js - 如何仅使用电子邮件 passport.js 对用户进行身份验证?

标签 node.js authentication passport.js

我正在尝试通过特定流程验证我的用户。

用户将输入电子邮件进行注册,用户将立即登录帐户,我将发送电子邮件确认链接,用户可以通过该链接更新密码并登录帐户。

但是我卡住了

我很困惑如何仅使用电子邮件注册和登录

路由.js

  app.post('/get_started', (req, res) => {
         let {email} = req.body;
         let isEmail = emailValidator(email);
         // i will save the user email and try to login
         if(isEmail){
             passport.authenticate('local',{ successRedirect: '/dashboard', failureRedirect: '/' })
         }
    })

Startegy.js

import LocalStrategy from 'passport-local'

export default passport => {

    passport.use('local',new LocalStrategy( (email, done) => {

        console.log(email,'Inside Passport')

    }));

}

最佳答案

这是一个额外使用 sequelize 和 bcrypt 的例子:

路由.js:

app.post('/get_started', passport.authenticate('local'), (req, res) => { 
    // Do stuff after successful login here
}

Strategy.js:

passport.use(
    'local',
    new LocalStrategy(
        {usernameField: 'email'},
        (email, passwordCleartext, done) => 
            User.findOne({where: {email: email}})
                .then(user => 
                    !user ?
                        done('Unauthorized', false, {message: 'Incorrect email or password.'}) :
                        bcrypt.compare(passwordCleartext, user.password)
                            .then(res => 
                                res ? 
                                    done(null, user, {message: 'Logged In Successfully'}) :
                                    done('Unauthorized', false, {message: 'Incorrect email or password.'}))
                            )
                .catch(() => done('Unauthorized', false, {message: 'Incorrect email or password.'}))
    )
);

关于node.js - 如何仅使用电子邮件 passport.js 对用户进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51801440/

相关文章:

javascript - 错误 : Route. put() 需要回调函数,但得到了 [对象未定义]

java - iFrame 身份验证

node.js - 如何显示弹出窗口而不是重定向到 Facebook?

node.js - 需要 Mongoose.js 上的几个列之一

mysql - 如何在 Sequelize 中创建必填字段?

mongodb - Node -mongodb- native : storing references to IDs

node.js - MarkLogic Node.js 按 "last-modified"排序

react-native - 使用 Spotify 登录后无法返回我的应用程序

java - 如何存储 Android 的单用户登录数据?

node.js - 错误 : failed to find request token in session