Reputation: 837
How to implement JWT token refresh with flutter?
which package i need to use?
Can that be implemented with the HTTP package?
What i have tried
getauth(String username, String password) async {
http.post(url, body: {
'username': "atom",
'password': "abi",
'grant_type': "password",
'client_id': "2LJhXdj2Cu0LlVqHk2ilm1WWHtdwK**********",
'client_secret':
"buQIh6tLPC0hG6QZUMm7yP7QuSjBLiffTBx3zY2HYxw95ssXQ4F85ttE5fGHInm1LBkk9GhGiHZCvoR21l4bqIOQTTJBo0nJ5******************"
}).then((response) {
Map<String, dynamic> responseMap = json.decode(response.body);
if (response.statusCode == 200) {
print(response.body);
}
print(response.body);
});
}
Upvotes: 0
Views: 394
Reputation: 1968
Use http_interceptor and implement expired token retry policy
Exp.
class ExpiredTokenRetryPolicy extends RetryPolicy {
@override
Future<bool> shouldAttemptRetryOnResponse(ResponseData response) async {
if (response.statusCode == 401) {
// Perform your token refresh here.
return true;
}
return false;
}
}
Interceptors allow us to intercept incoming or outgoing HTTP requests using the HttpClient . By intercepting the HTTP request, we can modify or change the value of the request
Upvotes: 1