不断弹出此错误。检查并重新检查我的代码。
这是我的passport.ts
import * as express from 'express';
import * as passport from 'passport';
import * as session from 'express-session';
let MySQLStore = require('express-mysql-session')(session);
import { Strategy as LocalStrategy } from 'passport-local';
import * as userProc from '../procedures/users.proc';
import { pool } from './db';
export default function configurePassport(app: express.Express) {
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
}, (email, password, done) => {
userProc.readByEmail(email)
.then((user) => {
if (!user) {
return done(null, false, { message: 'Invalid login' });
}
if (user.password !== password) {
return done(null, false, { message: 'Invalid login' });
}
return done(null, user);
}, (err) => {
return done(err);
});
}));
我只导入策略,从护照本地作为LocalStrategy,在我导入护照的控制器中但由于某种原因它会抛出此错误。以下是我的控制器代码中的片段,我正在尝试验证本地策略。
router.post('/login', (req, res, next) => {
passport.authenticate('local', (err: any, user: models.IUser, info: any)
=> {
if (err) {
console.log(err);
return res.sendStatus(500);
}
if (!user) {
return res.sendStatus(401);
}
req.logIn(user, (err) => {
if (err) {
console.log(err);
return res.sendStatus(500);
} else {
delete user.password;
return res.send(user);
}
});
})(req, res, next);
});