Added Error handling

This commit is contained in:
Mohammad Salameh
2024-03-10 08:56:06 +03:00
parent d70c370c2e
commit 5a7787b42d
7 changed files with 37 additions and 128 deletions
+16 -9
View File
@@ -6,6 +6,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/auth/model/token.dart';
import 'package:syncrow_app/features/auth/model/user_model.dart';
import 'package:syncrow_app/services/api/authentication_api.dart';
import 'package:syncrow_app/services/api/network_exception.dart';
import 'package:syncrow_app/utils/helpers/decode_base64.dart';
part 'auth_state.dart';
@@ -47,17 +48,23 @@ class AuthCubit extends Cubit<AuthState> {
password: passwordController.text,
);
emit(AuthSuccess());
if (token.accessTokenIsNotEmpty) {
final parts = token.accessToken.split('.');
if (parts.length != 3) {
throw Exception('invalid access token');
}
final payload = decodeBase64(parts[1]);
final payloadMap = json.decode(payload); //Map dictionary
user = UserModel.fromToken(payloadMap);
emit(AuthSuccess());
} else {
emit(AuthError('Something went wrong'));
}
} catch (e) {
if (e is ServerFailure) {
emit(AuthError(e.errMessage));
}
emit(AuthError(e.toString()));
}
final parts = token.accessToken.split('.');
if (parts.length != 3) {
throw Exception('invalid access token');
}
final payload = decodeBase64(parts[1]);
final payloadMap = json.decode(payload); //Map dictionary
user = UserModel.fromToken(payloadMap);
}
}
+2
View File
@@ -11,6 +11,8 @@ class Token {
: accessToken = '',
refreshToken = '';
bool get accessTokenIsNotEmpty => accessToken.isNotEmpty;
Token(
this.accessToken,
this.refreshToken,