Files
eatvio_app/lib/features/auth/presentation/bloc/auth_cubit.dart
2025-02-17 12:41:43 +03:00

40 lines
1.3 KiB
Dart

import 'package:eatvio_app/core/app_router.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/token_storage.dart';
import 'package:eatvio_app/features/auth/presentation/bloc/auth_state.dart';
import 'package:eatvio_app/features/auth/repositories/auth_repository.dart';
import 'package:eatvio_app/features/bottom_navigation_bar/presentation/bloc/profile_avatar_cubit/profile_avatar_cubit.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(AuthState());
getTokenByCredentials(String email, String password) async {
emit(AuthState.loading());
try {
final token = (await authRepository.getTokenByCredentials(
email,
password,
))
.token;
print(token);
emit(
AuthState.loaded(
token: token,
),
);
await TokenStorage.saveTokenInStorage(token);
await sl<ProfileAvatarCubit>().getMyProfile();
AppRouter.navigatorKey.currentContext?.go('/my-profile-screen');
} catch (e) {
print(e);
emit(AuthState.error(errorMessage: '$e'));
}
}
}