refactor
This commit is contained in:
57
lib/features/auth/bloc/auth_bloc.dart
Normal file
57
lib/features/auth/bloc/auth_bloc.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
part 'auth_events.dart';
|
||||
part 'auth_state.dart';
|
||||
|
||||
class AuthBloc extends Bloc<AuthEvent, AuthState> {
|
||||
final _supabase = GetIt.instance<SupabaseClient>();
|
||||
|
||||
AuthBloc()
|
||||
: super(const AuthState(status: AuthStatus.initial, isLoginMode: true)) {
|
||||
on<ToggleAuthMode>(
|
||||
(event, emit) => emit(state.copyWith(isLoginMode: !state.isLoginMode)),
|
||||
);
|
||||
on<LoginRequested>((event, emit) async {
|
||||
emit(state.copyWith(status: AuthStatus.loading));
|
||||
try {
|
||||
if (state.isLoginMode) {
|
||||
// --- LOGICA LOGIN ---
|
||||
await _supabase.auth.signInWithPassword(
|
||||
email: event.email,
|
||||
password: event.password,
|
||||
);
|
||||
// Non serve emettere success qui, ci pensa il SessionBloc!
|
||||
} else {
|
||||
// --- LOGICA SIGNUP ---
|
||||
await _supabase.auth.signUp(
|
||||
email: event.email,
|
||||
password: event.password,
|
||||
// Qui potresti passare il "Codice Negozio" nei data dell'utente
|
||||
data: {'store_code': event.storeCode},
|
||||
);
|
||||
|
||||
// Nota: Se Supabase richiede conferma email, l'utente non sarà
|
||||
// loggato subito. Gestiamolo con un messaggio.
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: AuthStatus.success,
|
||||
error: "Controlla la tua email per confermare l'account!",
|
||||
),
|
||||
);
|
||||
}
|
||||
} on AuthException catch (e) {
|
||||
emit(state.copyWith(status: AuthStatus.failure, error: e.message));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: AuthStatus.failure,
|
||||
error: "Errore imprevisto: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
24
lib/features/auth/bloc/auth_events.dart
Normal file
24
lib/features/auth/bloc/auth_events.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
part of 'auth_bloc.dart';
|
||||
|
||||
abstract class AuthEvent extends Equatable {
|
||||
const AuthEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class ToggleAuthMode extends AuthEvent {} // Passa da Login a Registrazione
|
||||
|
||||
class LoginRequested extends AuthEvent {
|
||||
final String email;
|
||||
final String password;
|
||||
final String? storeCode;
|
||||
const LoginRequested({
|
||||
required this.email,
|
||||
required this.password,
|
||||
this.storeCode,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [email, password, storeCode];
|
||||
}
|
||||
26
lib/features/auth/bloc/auth_state.dart
Normal file
26
lib/features/auth/bloc/auth_state.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
part of 'auth_bloc.dart';
|
||||
|
||||
enum AuthStatus { initial, loading, success, failure }
|
||||
|
||||
class AuthState extends Equatable {
|
||||
const AuthState({
|
||||
required this.status,
|
||||
this.error,
|
||||
required this.isLoginMode,
|
||||
});
|
||||
|
||||
final AuthStatus status;
|
||||
final String? error;
|
||||
final bool isLoginMode;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, error, isLoginMode];
|
||||
|
||||
AuthState copyWith({AuthStatus? status, String? error, bool? isLoginMode}) {
|
||||
return AuthState(
|
||||
status: status ?? this.status,
|
||||
error: error,
|
||||
isLoginMode: isLoginMode ?? this.isLoginMode,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user