this._verify is not a function on login

試著忘記壹切 提交于 2021-01-29 05:11:17

问题


I am trying to make a CRUD app for my portfolio. I want the user to be able to sign up and login and then to some stuff once inside (its a calories tracker). The user can Sign Up but when I try to login, I get:

this.verify is not a function

Here is the login code:

// Authenticate Login
app.post("/login", (req, res, next) => {
    passport.authenticate("local", {
        successRedirect: "/myprofile",
        failureRedirect: "/login",
        failureFlash: true
    })(req, res, next);
});

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

If someone is interested or you need some more code I can of course post it, I am not sure what else to include. But if you are feeling like it, here is the link to the GitHub Repo.

Thank you for all your help, Stay well and healthy

Edit1: Fullstack of the question (or I hope this is it)

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)

回答1:


If you use the option passReqToCallback passport expects the req-object to be passed as the first argument in the callback:

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'passwd',
    passReqToCallback: true,
  },
  function(req, username, password, done) {
    // request object is now first argument
    // ...
  }
));

See this for more information.



来源:https://stackoverflow.com/questions/62851981/this-verify-is-not-a-function-on-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!