Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of express-session and JWT

I have a bunch of questions about express-session and JWT in a project that I am building.

I have an Express API server that I want to protect using some sort of API key, to make sure only authorized applications can access to my data. JWT would probably get the job done. However I also need to authenticate users and restrict them from accessing certain parts of the data (e.g. role-based permissions) using express-session.

The frontend server would be a Next.js instance, which would save and use the cookies for express-session. The session would be stored in a MongoDB instance.

Would I be able to use both authentication methods in the same project? Would it be secure? Is there any easier approach to this? How could I implement the permissions?

Any help and tips would be appreciated.

like image 563
Alejandro Otero Gómez Avatar asked Dec 13 '18 08:12

Alejandro Otero Gómez


1 Answers

JWT and Express-Session both accomplish the same thing. The difference is a browser does not allows a http-only cookie to be accessible through javascript. At then end they are both used for the same end.

The jwt should be related to a session of a user, therefore the users permissions are the ones that matter. These can be implemented in a DBs and related to the user. Does he has this permission or does his role has this permission is the middleware you would put on the routes.

In case of doing it with express-session, I would personally take the same approach.

like image 109
zardilior Avatar answered Nov 08 '22 21:11

zardilior