2026-04-22 11:06:02 +02:00
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
import 'package:flux/core/blocs/session/session_cubit.dart';
|
2026-05-20 11:03:33 +02:00
|
|
|
import 'package:flux/core/enums_and_consts/consts.dart';
|
2026-05-04 15:36:42 +02:00
|
|
|
import 'package:flux/core/utils/app_message.dart';
|
2026-04-22 11:06:02 +02:00
|
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
|
part 'auth_state.dart';
|
|
|
|
|
|
|
|
|
|
class AuthCubit extends Cubit<AuthState> {
|
|
|
|
|
final _supabase = GetIt.instance<SupabaseClient>();
|
|
|
|
|
|
|
|
|
|
AuthCubit() : super(const AuthState());
|
|
|
|
|
|
|
|
|
|
void toggleMode() {
|
|
|
|
|
emit(state.copyWith(isLoginMode: !state.isLoginMode));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 12:49:04 +02:00
|
|
|
Future<bool> submitAuth(String email, String password) async {
|
|
|
|
|
// <-- Modificato in bool
|
2026-04-22 11:06:02 +02:00
|
|
|
// Partiamo puliti: via vecchi messaggi ed errori
|
|
|
|
|
emit(state.copyWith(status: AuthStatus.loading));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (state.isLoginMode) {
|
|
|
|
|
// --- LOGICA LOGIN ---
|
|
|
|
|
await _supabase.auth.signInWithPassword(
|
|
|
|
|
email: email,
|
|
|
|
|
password: password,
|
|
|
|
|
);
|
2026-05-25 12:49:04 +02:00
|
|
|
|
|
|
|
|
// Il login è andato a buon fine!
|
|
|
|
|
emit(
|
|
|
|
|
AuthState(
|
|
|
|
|
status: AuthStatus.initial,
|
|
|
|
|
isLoginMode: true,
|
|
|
|
|
errorMessage: null,
|
|
|
|
|
infoMessage: null,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return true;
|
2026-04-22 11:06:02 +02:00
|
|
|
} else {
|
|
|
|
|
// --- LOGICA SIGNUP ---
|
|
|
|
|
final AuthResponse res = await _supabase.auth.signUp(
|
|
|
|
|
email: email,
|
|
|
|
|
password: password,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (res.session == null) {
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(
|
|
|
|
|
status: AuthStatus.initial,
|
2026-05-04 15:36:42 +02:00
|
|
|
infoMessage: AppMessage(
|
|
|
|
|
key: 'authCubitCheckEmailToConfirmAccount',
|
|
|
|
|
),
|
2026-04-22 11:06:02 +02:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
emit(state.copyWith(status: AuthStatus.initial));
|
|
|
|
|
GetIt.I<SessionCubit>().initializeSession();
|
|
|
|
|
}
|
2026-05-25 12:49:04 +02:00
|
|
|
|
|
|
|
|
// Anche la registrazione è andata a buon fine!
|
|
|
|
|
emit(
|
|
|
|
|
AuthState(
|
|
|
|
|
status: AuthStatus.initial,
|
|
|
|
|
isLoginMode: true,
|
|
|
|
|
errorMessage: null,
|
|
|
|
|
infoMessage: null,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return true;
|
2026-04-22 11:06:02 +02:00
|
|
|
}
|
|
|
|
|
} on AuthException catch (e) {
|
|
|
|
|
emit(state.copyWith(status: AuthStatus.failure, errorMessage: e.message));
|
2026-05-25 12:49:04 +02:00
|
|
|
return false; // <-- Il login è fallito
|
2026-04-22 11:06:02 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(
|
|
|
|
|
status: AuthStatus.failure,
|
|
|
|
|
errorMessage: "Errore imprevisto: $e",
|
|
|
|
|
),
|
|
|
|
|
);
|
2026-05-25 12:49:04 +02:00
|
|
|
return false; // <-- Il login è fallito
|
2026-04-22 11:06:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 15:33:38 +02:00
|
|
|
Future<void> requestPasswordReset(String email) async {
|
|
|
|
|
if (email.isEmpty) {
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(
|
|
|
|
|
status: AuthStatus.failure,
|
|
|
|
|
errorMessage: 'Devi inserire l\'indirizzo email',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await _supabase.auth.resetPasswordForEmail(
|
|
|
|
|
email,
|
|
|
|
|
redirectTo: resetPasswordUrl,
|
|
|
|
|
);
|
|
|
|
|
emit(
|
|
|
|
|
state.copyWith(
|
|
|
|
|
status: AuthStatus.pwResetSent,
|
2026-05-04 15:36:42 +02:00
|
|
|
infoMessage: AppMessage(
|
|
|
|
|
key: 'authCubitResetPasswordEmailSentTo',
|
|
|
|
|
argument: email,
|
|
|
|
|
),
|
2026-04-28 15:33:38 +02:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 11:06:02 +02:00
|
|
|
Future<void> requestLogout() async {
|
|
|
|
|
await _supabase.auth.signOut();
|
|
|
|
|
emit(state.copyWith(status: AuthStatus.initial));
|
|
|
|
|
}
|
|
|
|
|
}
|