mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 22:17:55 +00:00
Initialized Auth pages for future work
Implemented Login functionality
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
Reference in New Issue
Block a user