After introducing JWT in my own application. I am facing some issues, might be I do it in wrong way. Please Suggest me the best way of implementation.
Technology Stack : (MERN) MongoDB Expressjs React Node.
After successfully login , I am creating a new JWT token by adding "user-id" in to it and return back to UI layer. At UI end I am storing that token in session storage. This token I am using for all further requests to the server. Before going to the controller I am checking Token in middleware for validtaion by using JWT verify. if successfully verified then next() else return an error with an invalid token.
Issue accurs now :
- Register with USER 1
- Login with USER 1
- After successfully login copy Token from session storage.
- Then Logout USER 1
- Register with USER 2
- Login with USER 2
- Paste in session storage Token of USER 1 into USER 2
- After refreshing a page USER 1 dashboard again instead of USER 2.
Any help or suggestions for following two points:
- How should I manage user Session by JWT?
- How should I manage API Authentication by JWT?