Reputation: 2881
I am using reactjs on the client-side. The server is sending a cookie after authentication.
So my question is that How to get JWT cookie in react-js which is send by the server and how to manage a token on behalf of this cookie
Upvotes: 2
Views: 2654
Reputation: 389
Typically, there will be an API for client-side to get the data of the current user, for instance, send a GET
request to /api/me
to get the user data { username: 'harry830622', /* email, gender, etc. */ }
.
In your scenario, the JWT is in the cookie, so it will be sent to the server in every request made by the browser automatically.
When the server receive the request, it will first verify the JWT in the cookie, if the JWT is correct, respond with the data, otherwise, throw an error of 401 UNAUTHORIZED
.
Therefore, to check if a user is logined or not, simply send a GET
request to the API to see if the response has valid data.
Upvotes: 2