Bcrypt And PassportJs User Registration

I am not sure about if I am us,ng bcrypt correctly.

This is my code that I tried to write.

app.post('/kayıt/', function(req, res){
bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(password, salt, function(err, hash) {
        let user = new User({
        isim:req.body.isim,
        email:req.email.isim,
        password:req.body:password,
});
user.save(function(err){
if(err){
  console.log(err);
}else{
res.redirect('/');
});

Do you think that are there any errors ? Or the usage of bcrypt and user registration process made correctly.(Note: In my procet I have stuck in another code blocks about express-validator, so that I tried to write a code but I am not sure if it is correct or not)

So that can you tell me about these codes, if it is wrong what should I change, if it is not what elses are alternative to write this registration part?

Upvotes: 3

Views: 1820

Answers (1)

kedar sedai
kedar sedai

Reputation: 1722

There are multiple methods to hash a password using bcrypt.js. Like you can use Promise, async, sync. The one that you used is async that generates salt and hash on a separate function calls. One error in your code is it should be password:req.body.password you need to use dot(.) after body instead of colon(:).

Besides your code one of the methods to hash a password is

 router.post('/kayıt', async (req, res) => {
     //hashed password 
    const salt = await bcrypt.genSalt(10);
    const hashedPassword = await bcrypt.hash(req.body.password, salt);

    const user = new User({
       isim: req.body.isim,
       email: req.body.email,
       password: hashedPassword
    });
    user.save(function(err) {
             if (err) {
                console.log(err);
             } else {
                res.redirect('/');
               }
             });
         });

You can have a look here.

Upvotes: 3

Related Questions