auth
This commit is contained in:
80
lib/blocs/session/session_bloc.dart
Normal file
80
lib/blocs/session/session_bloc.dart
Normal file
@@ -0,0 +1,80 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/data/enums.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'dart:async';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
part 'session_events.dart';
|
||||
part 'session_state.dart';
|
||||
|
||||
class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
||||
StreamSubscription<AuthState>? _authSubscription;
|
||||
|
||||
SessionBloc() : super(const SessionState.unknown()) {
|
||||
on<AppStarted>((event, emit) {
|
||||
// 1. Controlla la sessione attuale al boot
|
||||
final session = _supabase.auth.currentSession;
|
||||
if (session != null) {
|
||||
add(UserChanged(session.user.id));
|
||||
} else {
|
||||
add(UserChanged(null));
|
||||
}
|
||||
|
||||
// 2. Ascolta i cambiamenti futuri (login, logout, token scaduto)
|
||||
_authSubscription = _supabase.auth.onAuthStateChange.listen((data) {
|
||||
final userId = data.session?.user.id;
|
||||
add(UserChanged(userId));
|
||||
});
|
||||
});
|
||||
|
||||
on<UserChanged>((event, emit) async {
|
||||
if (event.userId == null) {
|
||||
emit(SessionState.unauthenticated());
|
||||
return;
|
||||
}
|
||||
// 1. Controlla se l'utente ha una Company
|
||||
final company = await _supabase
|
||||
.from('company')
|
||||
.select()
|
||||
.eq('user_id', event.userId!)
|
||||
.maybeSingle();
|
||||
|
||||
if (company == null) {
|
||||
emit(SessionState.authenticatedNoCompany(event.userId!));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Controlla i negozi
|
||||
final stores = await _supabase
|
||||
.from('store')
|
||||
.select()
|
||||
.eq('company_id', company['id']);
|
||||
|
||||
if (stores.isEmpty) {
|
||||
emit(SessionState.authenticatedNoStore(event.userId!, company['id']));
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Tutto ok, gestiamo le SharedPreferences per il negozio
|
||||
final prefs = GetIt.I.get<SharedPreferences>();
|
||||
String? lastStoreId = prefs.getString(PrefKeys.lastStore.value);
|
||||
|
||||
// Se non c'è nelle SharedPreferences, prendi il primo della lista
|
||||
if (lastStoreId == null || !stores.any((s) => s['id'] == lastStoreId)) {
|
||||
lastStoreId = stores.first['id'];
|
||||
await prefs.setString('last_store_id', lastStoreId!);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
_authSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
class SharedPreferencesKeys {}
|
||||
10
lib/blocs/session/session_events.dart
Normal file
10
lib/blocs/session/session_events.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
part of 'session_bloc.dart';
|
||||
|
||||
abstract class SessionEvent {}
|
||||
|
||||
class AppStarted extends SessionEvent {}
|
||||
|
||||
class UserChanged extends SessionEvent {
|
||||
final String? userId;
|
||||
UserChanged(this.userId);
|
||||
}
|
||||
37
lib/blocs/session/session_state.dart
Normal file
37
lib/blocs/session/session_state.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
part of 'session_bloc.dart';
|
||||
|
||||
enum SessionStatus {
|
||||
unknown,
|
||||
unauthenticated,
|
||||
authenticatedNoCompany, // Loggato ma deve creare l'azienda
|
||||
authenticatedNoStore, // Ha l'azienda ma deve creare/scegliere il primo negozio
|
||||
ready,
|
||||
}
|
||||
|
||||
class SessionState extends Equatable {
|
||||
final SessionStatus status;
|
||||
final String? userId;
|
||||
final String? companyId;
|
||||
|
||||
const SessionState._({
|
||||
this.status = SessionStatus.unknown,
|
||||
this.userId,
|
||||
this.companyId,
|
||||
});
|
||||
const SessionState.unknown() : this._();
|
||||
const SessionState.unauthenticated()
|
||||
: this._(status: SessionStatus.unauthenticated);
|
||||
const SessionState.authenticatedNoCompany(String userId)
|
||||
: this._(status: SessionStatus.authenticatedNoCompany, userId: userId);
|
||||
const SessionState.authenticatedNoStore(String userId, String companyId)
|
||||
: this._(
|
||||
status: SessionStatus.authenticatedNoStore,
|
||||
userId: userId,
|
||||
companyId: companyId,
|
||||
);
|
||||
const SessionState.ready(String userId)
|
||||
: this._(status: SessionStatus.ready, userId: userId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, userId];
|
||||
}
|
||||
Reference in New Issue
Block a user