How to hash password before saving to db to be compatible with passport module (passport local)

前端 未结 3 1306
遇见更好的自我
遇见更好的自我 2020-12-15 10:04

I am using passport-local strategy of passport for authentication. In my express server, I am getting a register post request and I should save password to db for a new user

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 10:52

    Why should we go for hashing algorithm, when passport already provided it for us? I mean we just need to plugin the passport-local-mongoose to our user schema like: UserSchema.plugin(passportLocalMongoose) and then, inside the register route we just tell the passportLocalMongoose to do the hashing for us by using:

    User.register(new User({username:req.body.username}), req.body.password,function(err,newUser)
    { 
        if(err){
            something
        }else{
            something
        }
    )
    

    By doing above we don't need to take care of hashing and it will be done for us. Please correct me if I am wrong or got your question wrong.

提交回复
热议问题