46 lines
1.3 KiB
Dart
46 lines
1.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../models/profile.dart';
|
|
import 'auth_provider.dart';
|
|
import 'supabase_provider.dart';
|
|
|
|
final currentUserIdProvider = Provider<String?>((ref) {
|
|
final authState = ref.watch(authStateChangesProvider);
|
|
return authState.maybeWhen(
|
|
data: (state) => state.session?.user.id,
|
|
orElse: () => ref.watch(sessionProvider)?.user.id,
|
|
);
|
|
});
|
|
|
|
final currentProfileProvider = StreamProvider<Profile?>((ref) {
|
|
final userId = ref.watch(currentUserIdProvider);
|
|
if (userId == null) {
|
|
return const Stream.empty();
|
|
}
|
|
final client = ref.watch(supabaseClientProvider);
|
|
return client
|
|
.from('profiles')
|
|
.stream(primaryKey: ['id'])
|
|
.eq('id', userId)
|
|
.map((rows) => rows.isEmpty ? null : Profile.fromMap(rows.first));
|
|
});
|
|
|
|
final profilesProvider = StreamProvider<List<Profile>>((ref) {
|
|
final client = ref.watch(supabaseClientProvider);
|
|
return client
|
|
.from('profiles')
|
|
.stream(primaryKey: ['id'])
|
|
.order('full_name')
|
|
.map((rows) => rows.map(Profile.fromMap).toList());
|
|
});
|
|
|
|
final isAdminProvider = Provider<bool>((ref) {
|
|
final profileAsync = ref.watch(currentProfileProvider);
|
|
return profileAsync.maybeWhen(
|
|
data: (profile) => profile?.role == 'admin',
|
|
orElse: () => false,
|
|
);
|
|
});
|