From 97b411e0c5986386f16463ec28fb312c285102ac Mon Sep 17 00:00:00 2001 From: doman Date: Sun, 27 Aug 2023 00:02:33 +0200 Subject: [PATCH] [client] refresh token on 401 and 403 --- lib/client.dart | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/client.dart b/lib/client.dart index bd380ed..4f504b9 100644 --- a/lib/client.dart +++ b/lib/client.dart @@ -55,6 +55,11 @@ class ApiClient { headers: headers(forGet: true), ); + if (response.statusCode == 401 || response.statusCode == 403) { + await refresh(); + return await get(path); + } + if (response.statusCode != 200) { throw Exception('Response returned status code: ${response.statusCode}'); } @@ -69,6 +74,11 @@ class ApiClient { headers: headers(forLogin: forLogin), ); + if (response.statusCode == 401 || response.statusCode == 403) { + await refresh(); + return await post(path, body, forLogin: forLogin); + } + if (response.statusCode != 200) { throw Exception('Response returned status code: ${response.statusCode}'); } @@ -83,6 +93,11 @@ class ApiClient { headers: headers(), ); + if (response.statusCode == 401 || response.statusCode == 403) { + await refresh(); + return await delete(path); + } + if (response.statusCode != 200) { throw Exception('Response returned status code: ${response.statusCode}'); } @@ -95,6 +110,11 @@ class ApiClient { headers: headers(), ); + if (response.statusCode == 401 || response.statusCode == 403) { + await refresh(); + return await patch(path, body); + } + if (response.statusCode != 200) { throw Exception('Response returned status code: ${response.statusCode}'); }