1

I want pass the encrypted password into the object. So when API called password will be in the encrypted form.

app.post('/api/login', (req, res) => {

   const user = {
        id: 1,
        username: "Vinit",
        password: "12345" //Here I want to pass the encrypted password
    }

    jwt.sign({user: user}, 'secretkey', (err, token) => {
        res.json({
            token: token
        });
    });
});
3
  • you mean like passing req.body.password? Commented Aug 21, 2020 at 13:45
  • yes @David but in the encrypted format. Commented Aug 21, 2020 at 13:46
  • hash it with bcrypt libary. npmjs.com/package/bcrypt Commented Aug 21, 2020 at 13:48

2 Answers 2

1

If not already implemented you need to use third party library like bcryptjs. Then you can generate hashed password like this

const bcrypt = require('bcryptjs');
bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash("NON_HASHED_PASSWORD", salt, function(err, hash) {
        console.log(hash) //<- Hashed password
    });
});

Remember this process is async in nature.

Read more about it here

Sign up to request clarification or add additional context in comments.

1 Comment

Please: hashing and encrypting are different things.
1

Try passport.js - It has lots of strategies including jwt, google auth, facebook.. etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.