forked from eatvio/eatvio_app
43 lines
1.3 KiB
Dart
43 lines
1.3 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:eatvio_app/core/app_router.dart';
|
|
import 'package:eatvio_app/core/token_storage.dart';
|
|
import 'package:eatvio_app/core/utils/toasts.dart';
|
|
import 'package:eatvio_app/features/auth/presentation/bloc/auth_state.dart';
|
|
import 'package:eatvio_app/features/auth/repositories/auth_repository.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
class AuthCubit extends Cubit<AuthState> {
|
|
final AuthRepository authRepository;
|
|
|
|
AuthCubit({
|
|
required this.authRepository,
|
|
}) : super(const AuthState());
|
|
|
|
Future<bool> getTokenByCredentials(String email, String password) async {
|
|
emit(const AuthState.loading());
|
|
try {
|
|
final token = (await authRepository.getTokenByCredentials(
|
|
email,
|
|
password,
|
|
))
|
|
.token;
|
|
emit(
|
|
AuthState.loaded(
|
|
token: token,
|
|
),
|
|
);
|
|
showSuccessToast('Успешный вход');
|
|
await TokenStorage.saveTokenInStorage(token);
|
|
AppRouter.navigatorKey.currentContext?.go('/my-profile-screen');
|
|
return true;
|
|
} catch (e) {
|
|
log(e.toString());
|
|
showErrorToast('Неверный адрес электронной почты или пароль');
|
|
emit(AuthState.error(errorMessage: '$e'));
|
|
return false;
|
|
}
|
|
}
|
|
}
|