Bill
Bill

Reputation: 527

Refreshing token in Flutter

I am developing a Flutter application and I am using OAuth2 for authentication. The application can't be used if you are not logged in, it just shows a login screen and forces you to log in.

When I log in, I receive the following information from the authentication server:

When the access token is about to expire, I want to get a new one by sending refresh token to authentication server.

How would I implement the refresh token mechanism? I want to update the access token every time before it expires, even if user is not using the application (it is closed). If user needed to log in every time he opens the application, it would be very bad user experience. To avoid this, I want to refresh the token in background.

How can I achieve this to work on Android and iOS? Preferably without writing any native code for each of the platforms.

Upvotes: 6

Views: 10081

Answers (1)

Michael Werner
Michael Werner

Reputation: 41

You can use Future.delayed to refresh the token before the expiration.

You can also run this part of code in background with background processes but your application must be in background.

Upvotes: 2

Related Questions