I'm creating a backend that supports authentication with JWT tokens. I'm using the classic access token / refresh token combo.
- the access token is valid for 5 minutes and allows the users to perform some actions. It's not checked against the database, it's valid until it expires
- the refresh token is valid 1 week and can only be used to get a new access token
I'm enquiring about best practices here, when it comes to getting a new access token. As of now, I have a middleware on the backend side. The middleware checks the access token in the header of each request:
- if the access token is still valid, allow the request
- if the access token is expired, fetch the refresh token from a cookie (by the way, when the backend tries to access a cookie stored on the browser side, does it result in an additional query?). The refresh token is then checked against the database
- if a new access token was issued, it's returned to the client for subsequent queries
The advantages of the setup above IMO is that everything happens in one query.
Now, I'm basically wondering if:
- accessing the refresh token in the cookie results in an additional query?
- If I'm moving to another kind of client (like Flutter on Android), is it ok (in terms of security, best practices, etc) to send the refresh token in the header of each request?
I saw some examples where people send the refresh token only when they get 401 back from the backend (e.g: Flutter: How to Refresh token when token expires during http call?), but that means two queries.