2022-02-03 18:06:44 +02:00
|
|
|
import 'package:auto_route/auto_route.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2023-02-21 05:43:39 +02:00
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:immich_mobile/routing/router.dart';
|
|
|
|
import 'package:immich_mobile/shared/services/api.service.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
class AuthGuard extends AutoRouteGuard {
|
2022-07-13 14:23:48 +02:00
|
|
|
final ApiService _apiService;
|
|
|
|
AuthGuard(this._apiService);
|
2022-02-03 18:06:44 +02:00
|
|
|
@override
|
|
|
|
void onNavigation(NavigationResolver resolver, StackRouter router) async {
|
|
|
|
try {
|
2023-02-21 17:25:31 +02:00
|
|
|
// temporary fix for race condition that the _apiService
|
|
|
|
// get called before accessToken is set
|
2023-02-21 05:43:39 +02:00
|
|
|
var userInfoHiveBox = await Hive.openBox(userInfoBox);
|
|
|
|
var accessToken = userInfoHiveBox.get(accessTokenKey);
|
|
|
|
_apiService.setAccessToken(accessToken);
|
2022-07-13 14:23:48 +02:00
|
|
|
var res = await _apiService.authenticationApi.validateAccessToken();
|
|
|
|
|
|
|
|
if (res != null && res.authStatus) {
|
2022-02-03 18:06:44 +02:00
|
|
|
resolver.next(true);
|
2022-07-13 14:23:48 +02:00
|
|
|
} else {
|
|
|
|
router.replaceAll([const LoginRoute()]);
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-07-13 14:23:48 +02:00
|
|
|
debugPrint("Error [onNavigation] ${e.toString()}");
|
|
|
|
router.replaceAll([const LoginRoute()]);
|
|
|
|
return;
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|