I have an application developed using React in the front-end and ASP.Net Web API in the backend. I am using JWT for authorization. The process is
- When a user logs in and is authenticated, 2 tokens are sent to the front-end, access token and refresh token. An access token is the JWT and a refreshes token is a random string and a refresh token is stored in a database.
- For every subsequent call to APIs access token is attached in the header, I have an authentication filter that validates the access token.
- Once the access token is expired, a 401 status is thrown with the error message TokenExpired.
- Once the front-end receives 401, it calls the refresh token API to get the refresh token
The question I have is that I cannot have an authentication filter to validate the access token of refresh tokens API as it will throw 401 due to the expired access token, so I need to make the refresh token API to be anonymous so it does not hit authentication filter. If I make anonymous I am making a call to the database to get the refresh token stored for the user and compare it with the one I received from the front-end. So is it safe to make the refresh token API anonymous, if not what's the best way?