Answer the question
In order to leave comments, you need to log in
What is the algorithm for working with JWT token?
Good afternoon.
I am writing a test task for react. The application is a kanban board, similar to trello.com, contains several boards and cards that you can drag from board to board and change their order.
But I have no experience with JWT. I can not understand the algorithm of actions. I have only two pages - authorization and the actual page with the boards.
In addition to the API requests for working with cards (I dealt with them), there are three requests associated with the user.
The first request is to create a user:
Create user
Receive data, create new user and return it. Also generate and return JWT token that can be used for authenticated
requests
{
"username": "string",
"email": "[email protected]",
"password": "string"
}
{
"username": "string",
"email": "[email protected]",
"password": "string",
"token": "string"
}
{
"username": "string",
"password": "string"
}
{
"username": "string",
"token": "string"
}
{
"token": "string"
}
{
"token": "string"
}
Answer the question
In order to leave comments, you need to log in
During authorization, both tokens are issued at once, working and refresh. The refresh token is stored in the server database along with the user ID; there is no point in storing a working token.
The lifetime is written in the token itself, as one of the payload fields. Both the server and the client can read this field. The server in any case must control the lifetime of the token, the client can do it himself, or can simply respond to server responses.
Each request to the server is accompanied by a work token. If the working token has expired, the server returns a message about the need to renew the token.
The refresh request is accompanied by a refresh token.
If the refresh-token in the database is marked as already used, then all refresh-tokens of the given user are inactivated (removed from the database) and a message about the need for re-authorization is returned.
Refresh token is marked as used in the database.
If the refresh token has expired or such a refresh token is not in the server database, then a message is returned about the need for re-authorization.
A new pair of tokens is returned.
Where and how to store tokens on the client is a matter of preference. You can not store it at all, then when the page is reloaded, the user will have to log in again. It is possible to save only the refresh token by making a refresh request on application startup/page opening.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question