fix : auth middleware
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import 'package:auths/data/repositories/auth_repository_imp.dart';
|
||||
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
import '../di/auth_di.dart';
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:auths/data/common/constant.dart';
|
||||
import 'package:auths/data/repositories/auth_repository_imp.dart';
|
||||
import 'package:auths/data/services/auth_service.dart';
|
||||
import 'package:rasadyar_auth/data/common/constant.dart';
|
||||
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
||||
import 'package:rasadyar_auth/data/services/auth_service.dart';
|
||||
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
import '../common/dio_manager.dart';
|
||||
@@ -17,7 +18,8 @@ Future<void> setupAuthDI() async {
|
||||
() => AuthRepositoryImpl(dioRemote),
|
||||
);
|
||||
diAuth.registerLazySingleton(() => AuthService());
|
||||
|
||||
diAuth.registerLazySingleton(() => TokenStorageService());
|
||||
|
||||
//hive
|
||||
//await diAuth.registerCachedFactoryAsync(() async=>await ,)
|
||||
//await diAuth.registerCachedFactoryAsync(() async=>await ,)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rasadyar_auth/data/di/auth_di.dart';
|
||||
import 'package:rasadyar_auth/data/services/token_storage_service.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
import '../../presentation/routes/pages.dart';
|
||||
|
||||
class AuthMiddleware extends GetMiddleware{
|
||||
class AuthMiddleware extends GetMiddleware {
|
||||
var tokenService = diAuth.get<TokenStorageService>();
|
||||
|
||||
@override
|
||||
RouteSettings? redirect(String? route) {
|
||||
if(route == AuthPaths.auth) {
|
||||
return const RouteSettings(name: AuthPaths.moduleList);
|
||||
eLog('redirect');
|
||||
final refreshToken = tokenService.getRefreshToken();
|
||||
final accessToken = tokenService.getAccessToken();
|
||||
|
||||
if (refreshToken == null || accessToken == null) {
|
||||
return RouteSettings(name: AuthPaths.moduleList);
|
||||
}
|
||||
return super.redirect(route);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -43,11 +43,10 @@ class TokenStorageService extends GetxService {
|
||||
value: token,
|
||||
);
|
||||
|
||||
Future<String?> getAccessToken() async =>
|
||||
await _localStorage.read(boxName: _boxName, key: _accessTokenKey);
|
||||
|
||||
Future<String?> getRefreshToken() async =>
|
||||
await _localStorage.read(boxName: _boxName, key: _refreshTokenKey);
|
||||
String? getAccessToken() =>
|
||||
_localStorage.read<String?>(boxName: _boxName, key: _accessTokenKey);
|
||||
String? getRefreshToken() =>
|
||||
_localStorage.read<String?>(boxName: _boxName, key: _refreshTokenKey);
|
||||
|
||||
Future<void> deleteTokens() async => await _localStorage.clear(_boxName);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user