我是Sails JS的新手,并且已经成功建立了带有护照的身份验证。在对用户进行身份验证之后,res.send()可以发送用户信息,但是res.view()返回以下内容:
Sending 500 ("Server Error") response:
Could not render view "/dashboard". Tried locating view file @ "/Users/hackthievist/Projects/delivery-science-project/views/bad". Layout configured as "layouts/layout", so tried using layout @ "/Users/hackthievist/Projects/delivery-science-project/views/layouts/layout")
这是我的AuthController:
/**
* AuthController
*
* @description :: Server-side actions for handling incoming requests.
* @help :: See https://sailsjs.com/docs/concepts/actions
*/
const passport = require('passport');
module.exports = {
login: function(req, res) {
passport.authenticate('local', (err, user, info) => {
if((err) || (!user)) {
return res.send({
message: info.message,
user
});
}
req.logIn(user, (err) => {
if(err) res.send(err);
return res.view('dashboard');
});
})(req, res);
},
logout: function(req, res) {
req.logout();
res.redirect('/');
}
};
这是我的password.js
const passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
bcrypt = require('bcrypt-nodejs');
passport.serializeUser(function (user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function (id, cb) {
User.findOne({ id }, function (err, user) {
cb(err, users);
});
});
passport.use(new LocalStrategy({
usernameField: 'username',
passportField: 'password'
}, function (username, password, cb) {
User.findOne({ username: username }, function (err, user) {
if (err) return cb(err);
if (!user) return cb(null, false, { message: 'Username not found' });
bcrypt.compare(password, user.password, function (err, res) {
if (!res) return cb(null, false, { message: 'Invalid password'});
let userDetails = {
email: user.email,
username: user.username,
id: user.id
};
return cb(null, userDetails, { message: 'Login Successful' });
});
});
}));
我也尝试从其他控制器使用res.view(),并且返回相同的错误。请帮忙。
答案 0 :(得分:0)
您可能从req.login
中收到错误消息。我注意到您的代码在出现错误时不会停止:
req.login(user, (err) => {
if(err) res.send(err);
return res.view('dashboard');
});
但是如果发生错误,它将运行res.send
和res.view
。您应该将其更改为:
req.login(user, (err) => {
if(err) { return res.send(err); }
return res.view('dashboard');
});
如果这不能解决您的问题,我将跟踪您收到的非常具体的错误消息-正如@streleckify指出的,为什么它要寻找文件views/bad
?这可能是布局文件中的一部分吗?