-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathemail_verification.dart
More file actions
157 lines (130 loc) · 4.55 KB
/
email_verification.dart
File metadata and controls
157 lines (130 loc) · 4.55 KB
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
// 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_auth/firebase_auth.dart' as fba;
import 'package:app_links/app_links.dart';
import 'package:flutter/material.dart';
import 'dart:async';
/// All possible states of the email verification process.
enum EmailVerificationState {
/// An initial state of the email verification process.
unresolved,
/// A state that indicates that the user has not yet verified the email.
unverified,
/// A state that indicates that the user has cancelled the email verification
/// process.
dismissed,
/// A state that indicates that email is being sent.
sending,
/// A state that indicates that user needs to follow the link and the app
/// awaits a valid deep link.
pending,
/// A state that indicates that the verification email was successfully sent.
sent,
/// A state that indicates that the user has verified its email.
verified,
/// A state that indiicates that email verification failed. Likely the
/// received link is invalid and verification email should be sent again.
failed,
}
/// A [ValueNotifier] that manages the email verification process.
class EmailVerificationController extends ValueNotifier<EmailVerificationState>
with WidgetsBindingObserver {
/// {@macro ui.auth.auth_controller.auth}
final fba.FirebaseAuth auth;
final AppLinks _appLinks;
StreamSubscription<Uri>? _linkSubscription;
EmailVerificationController(this.auth, {AppLinks? appLinks})
: _appLinks = appLinks ?? AppLinks(),
super(EmailVerificationState.unresolved) {
final user = auth.currentUser;
if (user != null) {
if (user.emailVerified) {
value = EmailVerificationState.verified;
} else {
value = EmailVerificationState.unverified;
}
}
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
_linkSubscription?.cancel();
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
reload();
}
}
/// An instance of user that is currently signed in.
fba.User get user => auth.currentUser!;
/// Current [EmailVerificationState].
EmailVerificationState get state => value;
/// Contains an [Exception] if [state] is [EmailVerificationState.failed].
Exception? error;
bool _isMobile(TargetPlatform platform) {
return platform == TargetPlatform.android || platform == TargetPlatform.iOS;
}
/// Reloads firebase user and updates the [state].
Future<void> reload() async {
await user.reload();
if (user.email == null) {
value = EmailVerificationState.unresolved;
} else if (user.emailVerified) {
value = EmailVerificationState.verified;
} else {
value = EmailVerificationState.unverified;
}
}
/// Indicates that email verification process was cancelled.
void dismiss() {
value = EmailVerificationState.dismissed;
_linkSubscription?.cancel();
}
/// Sends an email with a link to verify the user's email address.
Future<void> sendVerificationEmail(
TargetPlatform platform,
fba.ActionCodeSettings? actionCodeSettings,
) async {
value = EmailVerificationState.sending;
try {
await user.sendEmailVerification(actionCodeSettings);
} on Exception catch (e) {
error = e;
value = EmailVerificationState.failed;
return;
}
if (_isMobile(platform)) {
value = EmailVerificationState.pending;
_linkSubscription?.cancel();
_linkSubscription = _appLinks.uriLinkStream.listen(
(Uri uri) async {
try {
final code = uri.queryParameters['oobCode'];
if (code != null) {
await auth.checkActionCode(code);
await auth.applyActionCode(code);
await user.reload();
value = EmailVerificationState.verified;
_linkSubscription?.cancel();
}
} on Exception catch (err) {
error = err;
value = EmailVerificationState.failed;
_linkSubscription?.cancel();
}
},
onError: (error) {
this.error = error is Exception ? error : Exception(error.toString());
value = EmailVerificationState.failed;
_linkSubscription?.cancel();
},
);
} else {
value = EmailVerificationState.sent;
}
}
}