this._verify 不是登录时的函数
this._verify is not a function on login
我正在尝试为我的投资组合制作一个 CRUD 应用程序。我希望用户能够注册并登录,然后进入一些东西(它是一个卡路里跟踪器)。用户可以注册,但是当我尝试登录时,我得到:
this.verify is not a function
这里是登录码:
// Authenticate Login
app.post("/login", (req, res, next) => {
passport.authenticate("local", {
successRedirect: "/myprofile",
failureRedirect: "/login",
failureFlash: true
})(req, res, next);
});
passport.js
const LocalStrategy = require("passport-local").Strategy,
bcrypt = require("bcryptjs");
// User Model
const User = require("../models/user");
module.exports = function(passport) {
passport.use(
new LocalStrategy({usernameField:"username"}, {passReqToCallback: true}, (username, password, done) => {
// Check if there is a user with this username
User.findOne({
username: username
}).then(user => {
if(!user) {
return done(null, false, {message: "There is not a user with this username"});
}
// Check if the password is correct
bcrypt.compare(password, user.password, (err, isMatch) => {
if(err) throw err
if(isMatch) {
return done(null, user)
} else {
return done(null, false, {message: "Password is invalid"})
}
});
})
.catch(err => console.log(err));
})
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user)
})
})
}
如果有人感兴趣或者您需要更多代码,我当然可以 post,我不确定还包括什么。但如果您喜欢,这里是 link 到 GitHub Repo。
感谢您的帮助,
保持健康
Edit1:问题的全栈(或者我希望是这样)
TypeError: this._verify is not a function
at Strategy.authenticate (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport-local\lib\strategy.js:90:12)
at attempt (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport\lib\middleware\authenticate.js:366:16)
at authenticate (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport\lib\middleware\authenticate.js:367:7)
at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\app.js:187:7
at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:275:10)
at methodOverride (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\method-override\index.js:79:5)
at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:335:12)
如果您使用选项 passReqToCallback
passport 期望 req
-object 作为回调中的第一个参数传递:
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'passwd',
passReqToCallback: true,
},
function(req, username, password, done) {
// request object is now first argument
// ...
}
));
有关详细信息,请参阅 this。
我正在尝试为我的投资组合制作一个 CRUD 应用程序。我希望用户能够注册并登录,然后进入一些东西(它是一个卡路里跟踪器)。用户可以注册,但是当我尝试登录时,我得到:
this.verify is not a function
这里是登录码:
// Authenticate Login
app.post("/login", (req, res, next) => {
passport.authenticate("local", {
successRedirect: "/myprofile",
failureRedirect: "/login",
failureFlash: true
})(req, res, next);
});
passport.js
const LocalStrategy = require("passport-local").Strategy,
bcrypt = require("bcryptjs");
// User Model
const User = require("../models/user");
module.exports = function(passport) {
passport.use(
new LocalStrategy({usernameField:"username"}, {passReqToCallback: true}, (username, password, done) => {
// Check if there is a user with this username
User.findOne({
username: username
}).then(user => {
if(!user) {
return done(null, false, {message: "There is not a user with this username"});
}
// Check if the password is correct
bcrypt.compare(password, user.password, (err, isMatch) => {
if(err) throw err
if(isMatch) {
return done(null, user)
} else {
return done(null, false, {message: "Password is invalid"})
}
});
})
.catch(err => console.log(err));
})
);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user)
})
})
}
如果有人感兴趣或者您需要更多代码,我当然可以 post,我不确定还包括什么。但如果您喜欢,这里是 link 到 GitHub Repo。
感谢您的帮助, 保持健康
Edit1:问题的全栈(或者我希望是这样)
TypeError: this._verify is not a function
at Strategy.authenticate (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport-local\lib\strategy.js:90:12)
at attempt (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport\lib\middleware\authenticate.js:366:16)
at authenticate (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\passport\lib\middleware\authenticate.js:367:7)
at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\app.js:187:7
at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:275:10)
at methodOverride (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\method-override\index.js:79:5)
at Layer.handle [as handle_request] (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Dell Vostro 3885\Documents\Programming\PersonalProjects\CalorieTracker\node_modules\express\lib\router\index.js:335:12)
如果您使用选项 passReqToCallback
passport 期望 req
-object 作为回调中的第一个参数传递:
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'passwd',
passReqToCallback: true,
},
function(req, username, password, done) {
// request object is now first argument
// ...
}
));
有关详细信息,请参阅 this。