Currently I am working in dev env on my local machine where I am storing passwords in plain text using MongoDB. I am using express-jwt
along with jsonwebtoken
for passing user data and authentication. I’ve researched bcryptjs
and bcrypt
and I would like to implement whichever is best for React, and Express, for passing hashed passwords to the database from the client. I have found resources for server side, but nothing for client side.
My question is then, what is the methodology for properly saving encrypted passwords on my server when they are passed from a client? How do I encrypt passwords client side then authenticate server side?
I have read some articles saying there is no need to encrypt client side due to ssl, but others say there is an absolute need to encrypt client side. What is the correct way, and how can I implement it on my React application?
2
Answers
You don’t decrypt passwords. You ask the user for the password, then you hash it and compare it to the stored hash one you saved. If they’re the same, then (assuming you have a secure hashing algorithm) the unencrypted versions must be the same also.
Using Bcryptjs, Express and MongoDB:
There is no need to encrpyt client side, you can pass the password as plain text to the server using a post request (through a form usually).
Assuming you have a ‘user’ schema which looks similar to this:
On register/sign up in the server, where you handle the request you would hash the user’s password like so:
4.Upon login request (which will also be a post through a form o the client), you will compare the passwords using bcrpyt.compare() function, and if successful, assign a JWT to the user like so, this method assumes the token will be stored in the Cookies.
hope it helps.
edit, hashing client-side:
There is a debate about this, and in some protocols, it’s even required to hash passwords on the client-side, in short, because SSL already encrypts everything that moves from client to server hashing on the client-side is pretty pointless and is not widely accepted today, even @ big companies. Bottom line, the added security is neglectable, and is not worth the trouble and exposing hashing logic to the client side