-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathauth_state.dart
282 lines (248 loc) · 8.72 KB
/
auth_state.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:flutter/widgets.dart';
import 'package:firebase_auth/firebase_auth.dart'
show AuthCredential, MultiFactorResolver, User;
/// An abstract class for all auth states.
/// [AuthState] transitions could be captured with an [AuthStateChangeAction]:
///
/// ```dart
/// SignInScreen(
/// actions: [
/// AuthStateChangeAction<SignedIn>((context, state) {
/// print(state.user!.displayName);
/// print(state.user!.emailVerified);
/// }),
/// ],
/// );
/// ```
///
/// You could also subscribe your widget to auth state transitions with
/// [AuthState.of]:
///
/// ```dart
/// AuthFlowBuilder<EmailAuthController>(
/// child: MyCustomWidget(),
/// );
///
///class MyCustomWidget extends StatelessWidget {
/// @override
/// Widget build(BuildContext context) {
/// final state = AuthState.of(context);
///
/// if (state is AwaitingEmailAndPassword) {
/// return EmailForm();
/// } else if (state is AuthFailed) {
/// return ErrorText(state);
/// } else if (state is SignedIn) {
/// return Text(state.user!.displayName);
/// } else {
/// return Text("Unknown state ${state.runtimeType}");
/// }
/// }
///}
/// ```
abstract class AuthState {
const AuthState();
/// Returns current [AuthState] of the auth flow.
/// Should be used only inside the widget that has an [AuthFlowBuilder] as
/// an ancestor. Use [maybeOf] if there is a chance that the widget is used
/// without [AuthFlowBuilder] as an ancestor.
static AuthState of(BuildContext context) => maybeOf(context)!;
/// Returns current [AuthState] of the auth flow.
/// Could return null if no [AuthFlowBuilder] was found up the widget tree.
///
/// See [AuthFlowBuilder] for more examples.
static AuthState? maybeOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<AuthStateProvider>()?.state;
}
/// {@template ui.auth.auth_state.uninitialized}
/// A default [AuthState] for many auth flows.
/// {@endtemplate}
class Uninitialized extends AuthState {
/// {@macrp ffui.auth.auth_state.uninitialized}
const Uninitialized();
}
/// {@template ui.auth.auth_state.signing_in}
/// Indicates that sign in is in progress.
/// Could be used to reflect the loading state on the ui.
///
/// See [AuthState] docs for usage examples.
/// {@endtemplate}
class SigningIn extends AuthState {
/// {@macro ui.auth.auth_state.signing_in}
const SigningIn();
}
/// {@template ui.auth.auth_state.credential_received}
/// Indicates that the auth credential was successfully received.
/// This is an intermediate state that should transition to either [SignedIn],
/// [CredentialLinked] or [AuthFailed] depending on [AuthAction].
/// Could be used to reflect the loading state on the ui.
///
/// See [AuthState] docs for usage examples.
/// {@endtemplate}
class CredentialReceived extends AuthState {
/// A credential that was received during auth flow.
final AuthCredential credential;
CredentialReceived(this.credential);
}
/// {@template ui.auth.auth_state.credential_linked}
/// Indicates that the auth credential was successfully linked with the
/// currently signed in user account.
///
/// See [AuthState] docs for usage examples.
/// {@endtemplate}
class CredentialLinked extends AuthState {
/// A credential that was linked with the currently signed in user account.
final AuthCredential credential;
/// An instance of the [User] the credential was associated with.
final User user;
/// {@macro ui.auth.auth_state.credential_linked}
CredentialLinked(this.credential, this.user);
}
/// {@template ui.auth.auth_state.auth_failed}
/// An [AuthState] that indicates that something went wrong during
/// authentication.
///
/// See [AuthState] docs for usage examples.
/// {@endtemplate}
class AuthFailed extends AuthState {
/// The error that occurred during authentication.
/// Often this is an instance of [FirebaseAuthException] that might contain
/// more details about the error.
///
/// There is an [ErrorText] widget that can be used to display error details
/// in human readable form.
final Exception exception;
/// {@macro ui.auth.auth_state.auth_failed}
AuthFailed(this.exception);
}
/// {@template ui.auth.auth_state.signed_in}
/// An [AuthState] that indicates that the user has successfully signed in.
///
/// See [AuthState] docs for usage examples.
/// {@endtemplate}
class SignedIn extends AuthState {
/// An instance of the [User] that was signed in.
final User? user;
/// {@macro ui.auth.auth_state.signed_in}
SignedIn(this.user);
}
/// {@template ui.auth.auth_state.different_sign_in_methods_found}
/// An [AuthState] that indicates that there are different auth providers
/// associated with an email that was used to authenticate.
///
/// See [AuthState] docs for usage examples.
/// {@endtemplate}
class DifferentSignInMethodsFound extends AuthState {
/// An email that has different auth providers associated with.
final String email;
/// An instance of the auth credential that was obtained during sign in flow.
/// Could be used to link with the user account after a sign in using on of
/// the available [methods].
final AuthCredential? credential;
/// A list of provider ids that were found for the [email].
final List<String> methods;
/// {@macro ui.auth.auth_state.different_sign_in_methods_found}
DifferentSignInMethodsFound(this.email, this.methods, this.credential);
}
/// {@template ui.auth.auth_state.fetching_providers_for_email}
/// An [AuthState] that indicates that there is a lookup of available providers
/// for an email in progress.
///
/// See [AuthState] docs for usage examples.
/// {@endtemplate}
class FetchingProvidersForEmail extends AuthState {
/// {@macro ui.auth.auth_state.fetching_providers_for_email}
const FetchingProvidersForEmail();
}
/// {@template ui.auth.auth_state.mfa_required}
/// An [AuthState] that indicates that multi-factor authentication is required.
/// {@endtemplate}
class MFARequired extends AuthState {
/// A multi-factor resolver that should be used to complete MFA.
final MultiFactorResolver resolver;
const MFARequired(this.resolver);
}
class AuthStateProvider extends InheritedWidget {
final AuthState state;
const AuthStateProvider({
Key? key,
required this.state,
required Widget child,
}) : super(key: key, child: child);
@override
bool updateShouldNotify(AuthStateProvider oldWidget) {
return state != oldWidget.state;
}
}
/// {@template ui.auth.auth_state.auth_state_transition}
/// A sub-type of the [Notification] that is used to notify about auth state
/// transitions. You could use [NotificationListener], but it is recommended
/// to use [AuthStateListener] instead.
/// {@endtemplate}
class AuthStateTransition<T extends AuthController> extends Notification {
/// Previous [AuthState].
final AuthState from;
/// Current [AuthState].
final AuthState to;
/// An instance of [AuthController] that could be used to perform further
/// actions of the auth flow.
final T controller;
/// {@macro ui.auth.auth_state.auth_state_transition}}
AuthStateTransition(this.from, this.to, this.controller);
}
typedef AuthStateListenerCallback<T extends AuthController> = bool? Function(
AuthState oldState,
AuthState state,
T controller,
);
/// {@template ui.auth.auth_state.auth_state_listener}
/// A [Widget] that could be used to listen auth state transitions.
///
/// For example, you could show a snackbar when some error occurs:
///
/// ```dart
/// AuthStateListener<EmailAuthController>(
/// child: LoginView(
/// actions: AuthAction.signIn,
/// providers: [EmailAuthProvider()],
/// ),
/// listener: (oldState, state, controller) {
/// if (state is AuthFailed) {
/// ScaffoldMessenger.of(context).showSnackBar(
/// SnackBar(content: ErrorText(exception: state.exception),
/// );
/// }
/// }
/// )
/// ```
/// {@endtemplate}
class AuthStateListener<T extends AuthController> extends StatelessWidget {
final Widget child;
final AuthStateListenerCallback<T> listener;
const AuthStateListener({
Key? key,
required this.child,
required this.listener,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return NotificationListener(
onNotification: (notification) {
if (notification is! AuthStateTransition<T>) {
return false;
}
return listener(
notification.from,
notification.to,
notification.controller,
) ??
false;
},
child: child,
);
}
}