mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 22:27:56 +00:00
Added Login with email model instead of json encode
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.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';
|
||||
import 'package:syncrow_app/utils/helpers/decode_base64.dart';
|
||||
|
||||
part 'auth_state.dart';
|
||||
|
||||
@@ -44,27 +43,21 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
emit(AuthLoading());
|
||||
try {
|
||||
token = await AuthenticationAPI.loginWithEmail(
|
||||
email: emailController.text.toLowerCase(),
|
||||
password: passwordController.text,
|
||||
model: LoginWithEmailModel(
|
||||
email: emailController.text.toLowerCase(),
|
||||
password: passwordController.text,
|
||||
),
|
||||
);
|
||||
|
||||
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);
|
||||
//Map dictionary
|
||||
user = UserModel.fromToken(token);
|
||||
emit(AuthSuccess());
|
||||
} else {
|
||||
emit(AuthError('Something went wrong'));
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is ServerFailure) {
|
||||
emit(AuthError(e.errMessage));
|
||||
}
|
||||
emit(AuthError(e.toString()));
|
||||
} on DioException catch (e) {
|
||||
throw ServerFailure.fromDioError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user