Api exception: invalid-refresh-token
Achudh opened this issue · comments
ApiException: apiUrl=https://<backendUrl>.nhost.run/v1/auth/token, statusCode=401, responseBody={status: 401, message: Invalid or expired refresh token, error: invalid-refresh-token}
Facing this issue when the session is automatically refreshed. I have a function which checks auth state and refreshes the token in the background.
///
Future<bool> isUserLoggedIn(BuildContext? context) async {
log(
"FILEPATH: lib/src/core/services/auth_service.dart\n",
name: "isUserLoggedIn()",
);
log(
"Entering function isUserLoggedIn()...\n",
name: "isUserLoggedIn()",
);
log(
"Reading refreshToken from SharedPreferences...\n",
name: "isUserLoggedIn()",
);
final String? storedRefreshToken = await _prefs.then(
(SharedPreferences value) => value.getString("refresh_token"),
);
if (storedRefreshToken == null) {
log(
"refreshToken not found from SharedPreferences...\n",
name: "isUserLoggedIn()",
);
isAuthenticated = false;
// notifyListeners();
return false;
} else if (!isAuthenticated) {
try {
log(
"refreshToken found from SharedPreferences attempting to signInWithRefreshToken...\n",
name: "isUserLoggedIn()",
);
log(
"Calling: nhostClient.auth.signInWithRefreshToken($storedRefreshToken)\n",
name: "isUserLoggedIn()",
);
final AuthResponse response =
await nhostClient.auth.signInWithRefreshToken(storedRefreshToken);
log(
"AuthResponse success; userId: ${response.user?.id}\n",
name: "isUserLoggedIn()",
);
await _prefs.then(
(SharedPreferences value) => value.setString(
"refresh_token",
response.session!.refreshToken ?? "",
),
// ignore: always_specify_types
onError: (e, s) {
log(e.toString(), error: e);
log(s.toString(), error: s);
},
);
log(
"SharedPreferences.write; refreshToken: ${response.session!.refreshToken}\n",
name: "isUserLoggedIn()",
);
accessToken = response.session!.accessToken!;
isAuthenticated = true;
userId = response.session!.user!.id;
notifyListeners();
} on ApiException catch (e, s) {
if (context != null) {
context
.read<SharedService>()
.showToast(message: e.body["message"].toString());
}
log("FILEPATH: lib/src/core/services/auth_service.dart 184:49");
log(
"ApiException ERROR on isUserLoggedIn: \n${e.body}\n - stack: $s",
name: "isUserLoggedIn()",
error: e.statusCode,
stackTrace: s,
);
} catch (e, s) {
if (context != null) {
context.read<SharedService>().showToast(message: e.toString());
}
log("FILEPATH: lib/src/core/services/auth_service.dart 194:49");
log(e.toString());
log(
"ERROR on isUserLoggedIn: $e - stack: $s",
name: "isUserLoggedIn()",
error: e,
stackTrace: s,
);
}
}
return isAuthenticated;
}
We have fixed this issue in our latest Beta version, you may check it out https://pub.dev/packages/nhost_sdk/versions/4.0.0-dev.9
You need to remove backendUrl
and add subdomain
and region
Hi @mhadaily big fan of yours! Thank you so much for all your contribution to open-source community! Can we expect a video tutorial of nHost with Flutter in your channel any time soon?
PS: Thank you for resolving the issue!
Will test it out
@Achudh Thank you my friend.
Yes, I have planned a complete course on Full stack Nhost and Flutter and will start recording as soon as I release version 4.0.0 Stable.
I don't think I'm facing the issue anymore. Thank you so much @mhadaily