mirror of
https://github.com/lucavenir/go_router_riverpod.git
synced 2025-08-06 14:59:53 +08:00
41 lines
970 B
Dart
41 lines
970 B
Dart
import 'dart:math';
|
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
|
|
import '../entities/user_role.dart';
|
|
import 'auth.dart';
|
|
|
|
part 'permissions.g.dart';
|
|
|
|
/// If our user is signed out, this provider returns [UserRole.none]
|
|
/// Otherwise, it mocks a network request and gives out some [UserRole].
|
|
@riverpod
|
|
Future<UserRole> permissions(PermissionsRef ref) async {
|
|
final userId = await ref.watch(
|
|
authNotifierProvider.selectAsync(
|
|
(value) => value.map(
|
|
signedIn: (signedIn) => signedIn.id,
|
|
signedOut: (signedOut) => null,
|
|
),
|
|
),
|
|
);
|
|
|
|
if (userId == null) return const UserRole.none();
|
|
|
|
return _requestMock();
|
|
}
|
|
|
|
/// Gives a random [UserRole] based on a dice roll.
|
|
UserRole _requestMock() {
|
|
// mock
|
|
final random = Random().nextDouble();
|
|
|
|
if (random < 0.25) {
|
|
return const UserRole.admin();
|
|
} else if (random < 0.5) {
|
|
return const UserRole.user();
|
|
} else {
|
|
return const UserRole.guest();
|
|
}
|
|
}
|