Menu

How to Store Passwords in Node.js the Right Way (with bcrypt)
📰

How to Store Passwords in Node.js the Right Way (with bcrypt)

DEV Community: javascript·hasnain alam·6 days ago
#6AKr9vZG
#devtojavascript#devto#dev#password#hash
Long article (888 words) — no AI summary yet.
Reading 0:00
15s threshold

When I built my first login system, I saved the password in the database exactly as the user typed it. It worked. Login was fine. I thought I was done.

I was not. If anyone had got into that database, they could have read every password in plain text. Worse, many people use the same password on other sites, so one leak can hurt them in many places.

So in this post, I will show you the simple way to do it in Node.js. It is only a few lines of code.

The one rule

Never save the real password. Save a hash of it.

A hash is a long string made from your password with a one-way function. You can turn a password into a hash, but you cannot turn the hash back into the password.

For example, the password mypassword123 might become something like this:

$2b$12$K8Hn3vYq0mZr1bQ5xLw9eOe7Jt2dUa4sVcPz6NfGhRiTyWuXoLkMa

Enter fullscreen mode

Exit fullscreen mode

You save that in your database. When the user logs in, you hash what they typed and compare it with the saved hash. If they match, the password is right.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.