import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:syncrow_app/features/auth/model/login_with_email_model.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'; part 'auth_state.dart'; class AuthCubit extends Cubit { AuthCubit() : super(AuthInitial()); static AuthCubit get(context) => BlocProvider.of(context); TextEditingController emailController = TextEditingController(); TextEditingController passwordController = TextEditingController(); bool isPasswordVisible = false; GlobalKey formKey = GlobalKey(); void changePasswordVisibility() { isPasswordVisible = !isPasswordVisible; emit(AuthPasswordVisibilityChanged()); } bool agreeToTerms = false; void changeAgreeToTerms() { agreeToTerms = !agreeToTerms; emit(AuthAgreeToTermsChanged()); } static UserModel? user; static Token token = Token.emptyConstructor(); // (FlutterSecureStorage().read(key :'token')); login() async { emit(AuthLoading()); try { token = await AuthenticationAPI.loginWithEmail( model: LoginWithEmailModel( email: emailController.text.toLowerCase(), password: passwordController.text, ), ); if (token.accessTokenIsNotEmpty) { FlutterSecureStorage storage = const FlutterSecureStorage(); await storage.write( key: Token.loginAccessTokenKey, value: token.accessToken); user = UserModel.fromToken(token); emit(AuthSuccess()); } else { emit(AuthError('Something went wrong')); } } on DioException catch (e) { emit(AuthError(e.message ?? "Something went wrong")); throw ServerFailure.fromDioError(e); } } logout() async { emit(AuthLoading()); try { FlutterSecureStorage storage = const FlutterSecureStorage(); await storage.delete(key: Token.loginAccessTokenKey); emit(AuthLoggedOut()); } on DioException catch (e) { throw ServerFailure.fromDioError(e); } } }