Skip to content

fix(ui_auth): Fix EmailVerificationController lifecycle #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/firebase_ui_auth/lib/src/email_verification.dart
Original file line number Diff line number Diff line change
@@ -57,6 +57,13 @@ class EmailVerificationController extends ValueNotifier<EmailVerificationState>
WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_disposed = true;
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
@@ -73,13 +80,21 @@ class EmailVerificationController extends ValueNotifier<EmailVerificationState>
/// Contains an [Exception] if [state] is [EmailVerificationState.failed].
Exception? error;

/// Controller might be disposed while [sendVerificationEmail] is waiting for
/// a future to complete. This flag is used to inform the method that it
/// should terminate early.
bool _disposed = false;

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 (_disposed) {
return;
}

if (user.email == null) {
value = EmailVerificationState.unresolved;
@@ -103,7 +118,15 @@ class EmailVerificationController extends ValueNotifier<EmailVerificationState>
value = EmailVerificationState.sending;
try {
await user.sendEmailVerification(actionCodeSettings);
// Controller might be disposed while waiting for the future to complete.
// In this case, avoid updating its value, as it would cause an exception.
if (_disposed) {
return;
}
} on Exception catch (e) {
if (_disposed) {
return;
}
error = e;
value = EmailVerificationState.failed;
return;
@@ -113,12 +136,27 @@ class EmailVerificationController extends ValueNotifier<EmailVerificationState>
value = EmailVerificationState.pending;
// ignore: deprecated_member_use
final linkData = await FirebaseDynamicLinks.instance.onLink.first;
if (_disposed) {
return;
}

try {
final code = linkData.link.queryParameters['oobCode']!;
await auth.checkActionCode(code);
if (_disposed) {
return;
}

await auth.applyActionCode(code);
if (_disposed) {
return;
}

await user.reload();
if (_disposed) {
return;
}

value = EmailVerificationState.verified;
} on Exception catch (err) {
error = err;
Original file line number Diff line number Diff line change
@@ -145,6 +145,12 @@ class __EmailVerificationScreenContentState
super.initState();
}

@override
void dispose() {
controller.dispose();
super.dispose();
}

void _sendEmailVerification(_) {
controller
..addListener(() {