Bug fixes, and updated the build number

This commit is contained in:
Abdullah Alassaf
2024-07-02 13:14:17 +03:00
parent 97e0f29ac6
commit c7d26d0e6e
5 changed files with 61 additions and 51 deletions

View File

@ -274,35 +274,39 @@ class AuthCubit extends Cubit<AuthState> {
}
getTokenAndValidate() async {
emit(AuthTokenLoading());
const storage = FlutterSecureStorage();
final firstLaunch =
await SharedPreferencesHelper.readBoolFromSP(StringsManager.firstLaunch) ?? true;
try {
emit(AuthTokenLoading());
const storage = FlutterSecureStorage();
final firstLaunch =
await SharedPreferencesHelper.readBoolFromSP(StringsManager.firstLaunch) ?? true;
if (firstLaunch) {
storage.deleteAll();
}
await SharedPreferencesHelper.saveBoolToSP(StringsManager.firstLaunch, false);
final value = await storage.read(key: Token.loginAccessTokenKey) ?? '';
if (value.isEmpty) {
emit(AuthTokenError(message: "Token not found"));
return;
}
final tokenData = Token.decodeToken(value);
if (tokenData.containsKey('exp')) {
final exp = tokenData['exp'] ?? 0;
final currentTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
if (currentTime < exp) {
emit(AuthTokenSuccess());
} else {
emit(AuthTokenError(message: "Token expired"));
if (firstLaunch) {
storage.deleteAll();
}
} else {
await SharedPreferencesHelper.saveBoolToSP(StringsManager.firstLaunch, false);
final value = await storage.read(key: Token.loginAccessTokenKey) ?? '';
if (value.isEmpty) {
emit(AuthTokenError(message: "Token not found"));
return;
}
final tokenData = Token.decodeToken(value);
if (tokenData.containsKey('exp')) {
final exp = tokenData['exp'] ?? 0;
final currentTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
if (currentTime < exp) {
emit(AuthTokenSuccess());
} else {
emit(AuthTokenError(message: "Token expired"));
}
} else {
emit(AuthTokenError(message: "Something went wrong"));
}
} catch (_) {
emit(AuthTokenError(message: "Something went wrong"));
}
}