-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_repository.dart
131 lines (107 loc) · 3.64 KB
/
auth_repository.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/// This library contains the authentication feature's data fetchers.
library;
import "dart:typed_data";
import "package:appwrite/appwrite.dart";
import "package:appwrite/models.dart";
import "package:riverpod_annotation/riverpod_annotation.dart";
import "../../../utils/api.dart";
import "../../../utils/log.dart";
import "../../utils/data/device_repository.dart";
import "../../utils/domain/device_model.dart";
import "../domain/account_type.dart";
import "../domain/pirate_user_entity.dart";
import "avatar_repository.dart";
part "auth_repository.g.dart";
/// A repository for authentication.
abstract interface class AuthRepository {
/// Authenticate the user.
Future<void> authenticate({required bool anonymous});
/// Get data about the current user.
Future<PirateUserEntity> getData();
}
/// The default implementation of [AuthRepository].
base class _AppwriteAuthRepository implements AuthRepository {
/// Create a new instance of [_AppwriteAuthRepository].
const _AppwriteAuthRepository(this.account, this.platform, this.avatarRepo);
/// The Appwrite [Account].
final Account account;
/// The [currentPlatform].
final Device platform;
/// The current user's avatar.
final AvatarRepository avatarRepo;
/// Get a user from Appwrite.
Future<User> getUser() => account.get();
@override
Future<PirateUserEntity> getData() async {
final user = await getUser();
return getUserData(user);
}
Future<PirateUserEntity> getUserData(User user) async {
final accountType = AccountType.fromEmail(user.email);
final avatar = await avatarRepo.getAvatar();
return PirateUserEntity(
name: user.name,
email: user.email,
accountType: accountType,
avatar: avatar,
isLoggedIn: true,
);
}
@override
Future<void> authenticate({bool anonymous = false}) async {
try {
await getUser();
} catch (e, s) {
log.warning("Failed to fetch session.", e, s);
await _createAccount(anonymous);
await getUser();
}
}
Future<void> _createAccount(bool anonymous) async {
if (anonymous) {
try {
await account.createAnonymousSession();
} catch (e, s) {
log.warning("Failed to create anonymous session.", e, s);
}
} else {
try {
// Go to the Google account login page.
switch (platform) {
// Both Android and iOS need the same behavior, so it reuses it.
case Device.android || Device.ios:
await account.createOAuth2Session(
provider: "google",
);
// TODO(lishaduck): The web needs different behavior than that of linux/mac/windows/fuchsia.
case Device.web ||
Device.linux ||
Device.macos ||
Device.windows ||
Device.other:
await account.createOAuth2Session(
provider: "google",
success: "${Uri.base.origin}/auth.html",
failure: "${Uri.base}",
);
}
} catch (e, s) {
log.warning("Failed to create OAuth2 session.", e, s);
}
}
}
}
/// The email address used in case things go wrong.
/// The name used in case things go wrong.
const redactedName = "Anonymous";
/// The avatar used in case things go wrong.
final redactedAvatar = Uint8List(1);
/// Get the authentication data provider.
@Riverpod(keepAlive: true)
AuthRepository auth(AuthRef ref) {
final account = ref.watch(accountsProvider);
final platform = ref.watch(currentPlatformProvider);
final avatar = ref.watch(avatarProvider);
return _AppwriteAuthRepository(account, platform, avatar);
}