chore : delete auth package because each project has different auth , must be separated auth logic
This commit is contained in:
23
packages/core/lib/data/services/auth_middelware.dart
Normal file
23
packages/core/lib/data/services/auth_middelware.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
|
||||
class AuthMiddleware extends GetMiddleware {
|
||||
final tokenService = Get.find<TokenStorageService>();
|
||||
final authRouteResolver = Get.find<AuthRouteResolver>();
|
||||
|
||||
@override
|
||||
RouteSettings? redirect(String? route) {
|
||||
final refreshToken = tokenService.refreshToken.value;
|
||||
final accessToken = tokenService.accessToken.value;
|
||||
final module = tokenService.appModule.value;
|
||||
|
||||
if (refreshToken == null || accessToken == null) {
|
||||
if (module != null) {
|
||||
final authRoute = authRouteResolver.getAuthRouteForModule(module);
|
||||
return RouteSettings(name: authRoute, arguments: module);
|
||||
}
|
||||
return RouteSettings(name: authRouteResolver.getFallbackRoute());
|
||||
}
|
||||
return super.redirect(route);
|
||||
}
|
||||
}
|
||||
2
packages/core/lib/data/services/services.dart
Normal file
2
packages/core/lib/data/services/services.dart
Normal file
@@ -0,0 +1,2 @@
|
||||
export 'auth_middelware.dart';
|
||||
export 'token_storage_service.dart';
|
||||
84
packages/core/lib/data/services/token_storage_service.dart
Normal file
84
packages/core/lib/data/services/token_storage_service.dart
Normal file
@@ -0,0 +1,84 @@
|
||||
import 'dart:convert';
|
||||
import 'package:rasadyar_core/core.dart';
|
||||
import 'package:rasadyar_core/hive_registrar.g.dart';
|
||||
|
||||
class TokenStorageService extends GetxService {
|
||||
static const String _tokenBoxName = 'TokenBox';
|
||||
static const String _appBoxName = 'AppBox';
|
||||
static const String _accessTokenKey = 'accessToken';
|
||||
static const String _refreshTokenKey = 'refreshToken';
|
||||
static const String _baseUrlKey = 'baseUrl';
|
||||
static const String _apiKey = 'apiKey';
|
||||
static const String _moduleKey = 'moduleSelected';
|
||||
|
||||
final FlutterSecureStorage _secureStorage = FlutterSecureStorage();
|
||||
final HiveLocalStorage _localStorage = diCore.get<HiveLocalStorage>();
|
||||
|
||||
RxnString accessToken = RxnString();
|
||||
RxnString refreshToken = RxnString();
|
||||
RxnString baseurl = RxnString();
|
||||
Rxn<Module> appModule = Rxn(null);
|
||||
|
||||
Future<void> init() async {
|
||||
await Hive.initFlutter();
|
||||
Hive.registerAdapters();
|
||||
|
||||
final String? encryptedKey = await _secureStorage.read(key: 'hive_enc_key');
|
||||
final encryptionKey = encryptedKey != null
|
||||
? base64Url.decode(encryptedKey)
|
||||
: Hive.generateSecureKey();
|
||||
|
||||
if (encryptedKey == null) {
|
||||
await _secureStorage.write(key: 'hive_enc_key', value: base64UrlEncode(encryptionKey));
|
||||
}
|
||||
|
||||
await _localStorage.init();
|
||||
await _localStorage.openBox(_tokenBoxName, encryptionCipher: HiveAesCipher(encryptionKey));
|
||||
await _localStorage.openBox(_appBoxName);
|
||||
|
||||
accessToken.value = _localStorage.read<String?>(boxName: _tokenBoxName, key: _accessTokenKey);
|
||||
refreshToken.value = _localStorage.read<String?>(boxName: _tokenBoxName, key: _refreshTokenKey);
|
||||
appModule.value = _localStorage.read<Module?>(boxName: _appBoxName, key: _moduleKey);
|
||||
baseurl.value = _localStorage.read<String?>(boxName: _appBoxName, key: _baseUrlKey);
|
||||
}
|
||||
|
||||
Future<void> saveAccessToken(String token) async {
|
||||
await _localStorage.save(boxName: _tokenBoxName, key: _accessTokenKey, value: token);
|
||||
accessToken.value = token;
|
||||
accessToken.refresh();
|
||||
}
|
||||
|
||||
Future<void> saveRefreshToken(String token) async {
|
||||
await _localStorage.save(boxName: _tokenBoxName, key: _refreshTokenKey, value: token);
|
||||
refreshToken.value = token;
|
||||
refreshToken.refresh();
|
||||
}
|
||||
|
||||
Future<void> saveModule(Module input) async {
|
||||
await _localStorage.save(boxName: _tokenBoxName, key: _moduleKey, value: input);
|
||||
appModule.value = input;
|
||||
appModule.refresh();
|
||||
}
|
||||
|
||||
Future<void> deleteTokens() async {
|
||||
await _localStorage.clear(_tokenBoxName);
|
||||
accessToken.value = null;
|
||||
refreshToken.value = null;
|
||||
}
|
||||
|
||||
Future<void> saveBaseUrl(String url) async {
|
||||
await _localStorage.save(boxName: _appBoxName, key: _baseUrlKey, value: url);
|
||||
baseurl.value = url;
|
||||
baseurl.refresh();
|
||||
}
|
||||
|
||||
void getBaseUrl() {
|
||||
var url = _localStorage.read(boxName: _appBoxName, key: _baseUrlKey);
|
||||
baseurl.value = url;
|
||||
baseurl.refresh();
|
||||
}
|
||||
|
||||
Future<void> saveApiKey(String key) async {
|
||||
await _localStorage.save(boxName: _tokenBoxName, key: _apiKey, value: key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user