400(坏请求)使用获取login

我正在尝试使用抓取到POSTlogin表单我的loginfunction使用护照。

当使用标准表单action =“/ login”时,login工作,但是我想要去取得能够显示实时错误输出,如果login失败,不刷新…

我得到400(坏请求)和Uncaught(在诺言)SyntaxError:意外的标记B在JSON在位置0时发送login请求…

这是我的代码:

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我得到未捕获(in promise) SyntaxError: Unexpected token B in JSON at position 0由于试图parsing.then()中的未定义的响应(in promise) SyntaxError: Unexpected token B in JSON at position 0 但我仍然得到400错误信息..我发送的数据错了?

编辑2

我修改了我的代码,现在我得到response.ok = true,但是我得到,即使我发送错误的login细节…我是否需要更新我的策略?

 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); });