Initialized Auth pages for future work

Implemented Login functionality
This commit is contained in:
Mohammad Salameh
2024-03-07 10:29:19 +03:00
parent 4087f9c71c
commit f734801e94
28 changed files with 728 additions and 110 deletions
+53
View File
@@ -1,7 +1,60 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
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/utils/helpers/decode_base64.dart';
part 'auth_state.dart';
class AuthCubit extends Cubit<AuthState> {
AuthCubit() : super(AuthInitial());
static AuthCubit get(context) => BlocProvider.of(context);
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
bool isPasswordVisible = false;
void changePasswordVisibility() {
isPasswordVisible = !isPasswordVisible;
emit(AuthPasswordVisibilityChanged());
}
bool agreeToTerms = false;
void changeAgreeToTerms() {
agreeToTerms = !agreeToTerms;
emit(AuthAgreeToTermsChanged());
}
UserModel? user;
Token token = Token.emptyConstructor();
// (FlutterSecureStorage().read(key :'token'));
login() async {
emit(AuthLoading());
try {
token = await AuthenticationAPI.loginWithEmail(
email: emailController.text.toLowerCase(),
password: passwordController.text,
);
emit(AuthSuccess());
} catch (e) {
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);
}
}
+16
View File
@@ -3,3 +3,19 @@ part of 'auth_cubit.dart';
abstract class AuthState {}
class AuthInitial extends AuthState {}
class AuthLoading extends AuthState {}
class AuthError extends AuthState {
final String message;
AuthError(this.message) {
debugPrint(message);
}
}
class AuthSuccess extends AuthState {}
class AuthPasswordVisibilityChanged extends AuthState {}
class AuthAgreeToTermsChanged extends AuthState {}