DEV Community

Cover image for Build a Login and Logout API using Express.js (Node.js)

Build a Login and Logout API using Express.js (Node.js)

Joshua M on March 15, 2023

Table of contents Introduction The Concept of Authentication and Authorization Authentication Authorization Setting up the devel...
Collapse
 
trener_107 profile image
Maksym Sokolov

Good article, thank you. But I found some bug in login logic. Need add await for this code, because isPasswordValid - always return Promise (true). Thanks again)

const isPasswordValid = await bcrypt.compare(
            `${req.body.password}`,
            user.password
        );

Enter fullscreen mode Exit fullscreen mode
Collapse
 
m_josh profile image
Joshua M

Correct! Thanks for pointing out Maksym.
My hands are presently full. Will edit the article once I have a free time

Collapse
 
mrmalik16 profile image
Sharjeel Faiq

I found that too. The guide is very useful and valuable. Please correct the mistake for the new learners. Thanks.

Collapse
 
mrmalik16 profile image
Sharjeel Faiq • Edited

If the command crypto.randomBytes(20).toString(‘hex’) does not generate a secret access token for you, then try the following one, i.e.

crypto.randomUUID(20).toString('hex')
'85ec4cea-2164-4ef7-ae91-b6d83208c3b9'

Collapse
 
m_josh profile image
Joshua M • Edited

Hi guys, I fixed the bug. Thank you all.
@trener_107 @mrmalik16

Collapse
 
aamin887 profile image
Amin Alhassan

That a really good post, good for beginners and for a refresher on how to put things together building an auth system in express.

Collapse
 
mrmalik16 profile image
Sharjeel Faiq

This is useful and amazing. Thanks a lot.

Collapse
 
hitheshkp profile image
hithesh

Thank you for the amazing content it really helped me

Collapse
 
asciidud3 profile image
asciidude

absolutely beautiful article 🙏 really helped with learning how JWTs work, thanks :D

Collapse
 
eeshal_teluri profile image
Eeshal Teluri

Thank you very very much Josh, for taking your time and writing this post.

Collapse
 
muhammad_umerqazi profile image
Muhammad Umer Qazi • Edited

Is there anyone have problem to check the authentication when user login and then try to authenticate the user it give the error session has expired and i just login.
what would be the possible reason ?

Image description

Collapse
 
m_josh profile image
Joshua M

@muhammad_umerqazi can you check your request header to be sure you're sending a valid token. Also check your generate token logic

Collapse
 
muhammad_umerqazi profile image
Muhammad Umer Qazi

Yes, there was the issue import the token, Now its working. Thanks for highlighting the point

Collapse
 
hseleiro profile image
Hugo Seleiro • Edited

Hello, thanks for this post. It was a very interesting day doing this! I have some stuff to point out.

On authorization logic, in the verify function the import from the token is not correct, we must import only the SECRET_ACCESS_TOKEN and not config. SECRET_ACCESS_TOKEN.

On the creation of VerifyRole, in VerifyRole function the async is missing.

Thank you.

Collapse
 
golbangi1000 profile image
DongWook Kim

hello why are
const app = express() , const server = express() ?

Collapse
 
m_josh profile image
Joshua M

@golbangi1000 I corrected the two server instances

Collapse
 
mrmalik16 profile image
Sharjeel Faiq

It doesn't matter how you name a variable, the app and the server variable will always be the same and behave the same way.

Collapse
 
moonknight1729 profile image
moonknight1729

nice article...

Collapse
 
m_josh profile image
Joshua M

I am glad this helped you. If you have any question regarding this, you may drop in the comment

Collapse
 
kumar1821 profile image
kumar

values imported from dotenv file are read "undefined". What should I do?

Collapse
 
kumar1821 profile image
kumar

Sorted out!! .env file was inside v1 folder.

Collapse
 
usmaanrangrez profile image
usmanrangrez

JWT are supposed to be stateless right?