gpt4 book ai didi

javascript - NodeJS - Passport 本地策略给了我 404

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:58 25 4
gpt4 key购买 nike

当我尝试使用 Passport 登录时,我非常头疼。我正在使用电子邮件和密码向/login 发出发布请求。 Passport 正确验证了它,err 未被调用,然后 return res.redirect('/user') 也被调用,但它返回 404 并且不重定向。我不知道为什么会发生这种情况。这是我的代码:

服务器(index.js):

const
express = require('express'),
app = express(),
mongoose = require('mongoose'),
passport = require('passport'),
cookieSession = require('cookie-session'),
bodyParser = require('body-parser'),
keys = require('./config/keys'),
user = require('./models/User'),
passportService = require('./services/passport'),
authRoutes = require('./routes/auth');

mongoose.connect(keys.mongoURI);

app.use(bodyParser.json());
app.use(
cookieSession({
maxAge: 15 * 24 * 60 * 60 * 1000,
keys: [keys.cookieKey]
})
);
app.use(passport.initialize());
app.use(passport.session());

passportService(passport);

authRoutes(app);

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});

passportService(passport.js):

const LocalStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');
const User = mongoose.model('users');

module.exports = (passport) => {
passport.serializeUser((user, done) => {
done(null, user._id);
});

passport.deserializeUser((id, done) => {
User.findById(id).then(user => {
done(null, user);
});
});

passport.use(
new LocalStrategy(
{
usernameField: 'emailaddr',
passwordField: 'passwd'
},
function(username, password, done) {

User.findOne({ email: username }, function(err, user) {

if(err){
return done(err);
}
if(!user) {
console.log('User not found with email: ', username);
return done(null, false);
}
if(user.password != password) {
console.log('Invalid password');
return done(null, false)
}

return done(null, user);
});

}));

}

身份验证路由:

const passport = require('passport');

module.exports = app => {

app.post('/api/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/user');
});
})(req, res, next);
});

}

/user 没有路由,因为我正在客户端中使用 React 和 React Router。请我需要你的帮助!!!

最佳答案

我建议不要在您的authenticate 函数上使用自定义回调。相反,请检查在调用 api 并运行身份验证后是否有用户附加到请求对象。

// calling redirects from your api will be problematic with react router
// allow your front end to decide what route to call based on the response

app.post('/api/login', passport.authenticate('local'), function(req, res, next) {
if(req.user) {
res.json(req.user);
} else {
// handle errors here, decide what you want to send back to your front end
// so that it knows the user wasn't found
res.statusCode = 503;
res.send({message: 'Not Found'})
}
});

关于javascript - NodeJS - Passport 本地策略给了我 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308165/

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