gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 23:26:08 24 4
gpt4 key购买 nike

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

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

但是我卡住了

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

路由.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/

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