Access token vs Refresh token in OAUTH2
Differentiate these 2 tokens to apply it in our software.
Access tokens carry the necessary information to access a resource directly. In other words, when a client passes an access token to a server managing a resource, that server can use the information contained in the token to decide whether the client is authorized or not. Access tokens usually have an expiration date and are short-lived.
Refresh tokens carry the information necessary to get a new access token. In other words, whenever an access token is required to access a specific resource, a client may use a refresh token to get a new access token issued by the authentication server. Common use cases include getting new access tokens after old ones have expired, or getting access to a new resource for the first time. Refresh tokens can also expire but are rather long-lived. Refresh tokens are usually subject to strict storage requirements to ensure they are not leaked. They can also be blacklisted by the authorization server.
- When you do log in, send 2 tokens (Access token, Refresh token) in response to the client.
- The access token will have less expiry time and Refresh will have long expiry time.
- The client (Front end) will store refresh token in his local storage and access token in cookies.
- The client will use an access token for calling APIs. But when it expires, pick the refresh token from local storage and call auth server API to get the new token.
- Your auth server will have an API exposed which will accept refresh token and checks for its validity and return a new access token.
- Once the refresh token is expired, the User will be logged out.
Thanks for reading this.