Node.js and Passport Object has no method validPassword

前端 未结 3 1947
说谎
说谎 2021-02-02 14:40

I\'m using Node.js + Express + Passport to create a simple authentication(local)

and what I\'ve reached so far that when a wrong username or password entered user is re

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 15:37

    You are using

    if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' });
    }
    

    but you haven't defined validPassword method. Attach it to your schema:

    var authSchema = mongoose.Schema({ 
        username: 'string',
        password: 'string'
    });
    authSchema.methods.validPassword = function( pwd ) {
        // EXAMPLE CODE!
        return ( this.password === pwd );
    };
    

    EDIT You've also incorrectly defined the schema. It should be:

    var authSchema = mongoose.Schema({ 
        username: String,
        password: String
    });
    

    Note that both username and password should be String type objects, not strings "string", if you know what I mean. :)

提交回复
热议问题