我正在尝试使用fetch来登录表单使用护照登录我的登录功能。
登录在使用标准表单action =“/ login”时有效,但是如果登录失败而没有刷新,我想过去获取能够显示实时错误输出...
我收到400(错误请求)和Uncaught(在promise中)语法错误:发送登录请求时,位置0的JSON中出现意外的令牌B ...
这是我的代码:
let payload = {
username: "user1@user1.se",
password: "superman123"
};
let data = new FormData();
data.append( "json", JSON.stringify( payload ) );
console.log(data);
fetch('/login', {
method: 'post',
body: data
}).then(function(response) {
// if (response.status >= 400) {
// throw new Error("Bad response from server");
// }
return response.json();
})
.then(function(data) {
console.log(data);
});
这是我的护照代码:
var passport = require('passport');
var Strategy = require('passport-local').Strategy;
var db = require('./db');
// Configure the local strategy for use by Passport.
//
// The local strategy require a `verify` function which receives the credentials
// (`username` and `password`) submitted by the user. The function must verify
// that the password is correct and then invoke `cb` with a user object, which
// will be set at `req.user` in route handlers after authentication.
passport.use(new Strategy(
function(username, password, cb) {
console.log("1");
db.users.findByUsername(username, function(err, user) {
if (err) { return cb(err); }
if (!user) { return cb(null, false); }
if (user.password != password) { return cb(null, false); }
return cb(null, user);
});
}));
// Configure Passport authenticated session persistence.
//
// In order to restore authentication state across HTTP requests, Passport needs
// to serialize users into and deserialize users out of the session. The
// typical implementation of this is as simple as supplying the user ID when
// serializing, and querying the user record by ID from the database when
// deserializing.
passport.serializeUser(function(user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
db.users.findById(id, function (err, user) {
if (err) { return cb(err); }
cb(null, user);
});
});
app.use(passport.initialize());
app.use(passport.session());
app.get('/',
require('connect-ensure-login').ensureLoggedIn("/login"),
function(req, res){
res.sendFile(path.join(__dirname+'/app/index.html'));
});
app.get('/login',
function(req, res){
res.sendFile(path.join(__dirname+'/app/public.html'));
});
app.post('/login',
passport.authenticate('local'),
function(req, res) {
console.log("post")
res.redirect('/');
});
app.get('/logout',
function(req, res){
req.logout();
res.redirect('/');
});
编辑1
由于尝试在.then()中解析未定义的响应,我得到了未捕获(in promise) SyntaxError: Unexpected token B in JSON at position 0
。但我仍然收到400错误消息..我发送数据错了吗?
编辑2
我修改了我的代码,现在我得到了response.ok = true,但即使我发送了错误的登录详细信息,我也明白了......我是否需要更新我的策略?
passport.use(new Strategy(
function(username, password, cb) {
db.users.findByUsername(username, function(err, user) {
if (err) { return cb(err); }
if (!user) { return cb(null, false); }
if (user.password != password) { return cb(null, false); }
return cb(null, user);
});
}));
app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
switch (req.accepts('html', 'json')) {
case 'html':
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/');
});
break;
case 'json':
if (err) { return next(err); }
if (!user) { return res.status(401).send({"ok": false}); }
req.logIn(user, function(err) {
if (err) { return res.status(401).send({"ok": false}); }
return res.send({"ok": true});
});
break;
default:
res.status(406).send();
}
})(req, res, next);
});