DEV Community

Cover image for Authentication in Node.js with MongoDB, bcrypt, and JWT web Tokens with cookies 🍪.

Authentication in Node.js with MongoDB, bcrypt, and JWT web Tokens with cookies 🍪.

Ritesh Kumar on August 21, 2021

Adding authentication to an application is one of the most challenging 😖 but also a very important part for developers, but today I will teach you ...
Collapse
 
charlyjazz profile image
Carlos Azuaje

JWT For session is a bad design.

Collapse
 
maswerdna profile image
Samson Andrew

Don't do that. It's discouraging!

Next time, try to say kudos for the write-up, then suggest something better 🙂

Collapse
 
nyctonio profile image
Ritesh Kumar

🥺 thank you

Collapse
 
nyctonio profile image
Ritesh Kumar • Edited

No this is not a session based authorization it is a token based but what I did is stored that token in the cookie rather than sending them through the Authorization header for every HTTP request we can do in that way too but I thought that cookie one will be easier to understand for beginners.

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

Nice post 👍👍...
I tend to use Express: app.use(express.json())

app.use(bodyparser.json()) outputs depreciation warnings here...just my preference though

Collapse
 
maswerdna profile image
Samson Andrew

You're right.
The recommended way is to call the methods directly on the express object.

Because...
In the latest version of Express, some bodyParser's methods have been added natively. That's why you don't have to install or require bodyParser again.

Collapse
 
nyctonio profile image
Ritesh Kumar

Yes you are right from now i will also use the same thanks for pointing it out 🤍

Collapse
 
maxinejs profile image
Maxine

@icecoffee is this the answer ?!

Collapse
 
icecoffee profile image
Atulit Anand • Edited

Here the author is talking about server side authentication.

Collapse
 
artis3n profile image
Ari Kalfus

Please take a look at auth0.com/blog/adding-salt-to-hash... to understand how to properly set a salt. A global value for your application is Not Good. The value does not need to be a secret, but it needs to be unique for every record you are hashing (unique per-password/user/record).

You typically store the salt with the hash either in a row in the same DB table or even prepended/appended to the hash with a delimeter. All that matters is the salt is unique per input.

From the Auth0 article:

A system-wide salt is pointless to mitigate attacks; it would just make passwords longer. A system-wide salt also easily allows an attacker to keep using hash tables. We should hash and salt each password created for a user. That is, we should generate a unique salt upon creation of each stored credential (not just per user or system-wide). That includes passwords created during registration or as the result of a password reset. If the user eventually cycles over the same password, we don't want to give away that the password has already been used.

Collapse
 
nyctonio profile image
Ritesh Kumar

Thank you very much learned something new 🥰.

Collapse
 
artis3n profile image
Ari Kalfus

Yeah!

Collapse
 
mthtanjim profile image
Tanjimul Hasan Tanjim

Can you explain about ":" which used in password:plainTextPassword

Collapse
 
nyctonio profile image
Ritesh Kumar • Edited

I am destructing req.body and storing password as plaintextpassword

image
you can understand this with this example

Collapse
 
mthtanjim profile image
Tanjimul Hasan Tanjim

Thanks you so much, I understood well.

Collapse
 
officialksolomon profile image
officialksolomon

On the area where you encrypted the password, after destructuring request body, you recreated the password.
Which is supposed to produce an error identifier already exist because const variable cannot be recreated.

Collapse
 
nyctonio profile image
Ritesh Kumar

Actually on destructuring the request body i am creating email and plaintextpassword and after that i created const password so there is no recreation 😇 thats why it didn't produced error.

Collapse
 
vyrru5 profile image
VyRru5

Why don’t use header authorization bearer in request ? Is the best practices

Collapse
 
nyctonio profile image
Ritesh Kumar • Edited

Yes we can implement it in that way too which will also make it not vulnerable to CSRF but I thought that cookie one will be more beginner friendly 😬.

Collapse
 
harshitaditya1 profile image
Harshit Aditya

Amazing Blog Ritesh.

Collapse
 
nyctonio profile image
Ritesh Kumar

Thanks harshit

Collapse
 
himanshu3651 profile image
Himanshu Singh

Quite Awesome....!!

Collapse
 
lakshay1025 profile image
Lakshay1025

Amazing pal...Great Job

Collapse
 
jayantsankhi profile image
jayantsankhi

Great work thanks for this article 👍😊

Collapse
 
harshitk816 profile image
Harshitk816

OMG! This literally made my work so easy-peasy😍. Thanks a lot for this!!! Looking forward to more posts like this💕

Collapse
 
dhruv_arora profile image
Dhruv Arora

You saved a life 🥺
I was stuck in this
Thank you brother ❤

Collapse
 
rishabh055 profile image
Rishabh Rathore

Such a Awesome Explanation Dude keep it up🔥✌🏻

Collapse
 
nyctonio profile image
Ritesh Kumar • Edited

Thanks Rishabh

Collapse
 
crackingdemon profile image
crackingdemon

No one has simple explanation than this post :) , worth spending time to read this , learned a lot :)