Skip to content

Commit 9b18093

Browse files
committed
docs(firebase_auth): replace deprecated fetchSignInMethodsForEmail in error handling docs
1 parent 889af7c commit 9b18093

1 file changed

Lines changed: 23 additions & 32 deletions

File tree

docs/auth/errors.md

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Project: /docs/_project.yaml
2-
Book: /docs/_book.yaml
1+
Project: /docs/\_project.yaml
2+
Book: /docs/\_book.yaml
33

44
<link rel="stylesheet" type="text/css" href="/styles/docs.css" />
55

@@ -51,49 +51,40 @@ try {
5151
} on FirebaseAuthException catch (e) {
5252
if (e.code == 'account-exists-with-different-credential') {
5353
// The account already exists with a different credential
54-
String email = e.email;
55-
AuthCredential pendingCredential = e.credential;
54+
String email = e.email!;
55+
AuthCredential pendingCredential = e.credential!;
5656
57-
// Fetch a list of what sign-in methods exist for the conflicting user
58-
List<String> userSignInMethods = await auth.fetchSignInMethodsForEmail(email);
57+
// Note: fetchSignInMethodsForEmail() is deprecated.
58+
// Instead, attempt sign-in directly with known providers
59+
// and handle the linking flow accordingly.
5960
60-
// If the user has several sign-in methods,
61-
// the first method in the list will be the "recommended" method to use.
62-
if (userSignInMethods.first == 'password') {
63-
// Prompt the user to enter their password
64-
String password = '...';
65-
66-
// Sign the user in to their account with the password
61+
// Try signing in with email/password if applicable
62+
try {
6763
UserCredential userCredential = await auth.signInWithEmailAndPassword(
6864
email: email,
69-
password: password,
65+
password: promptUserForPassword(), // prompt user for password
7066
);
71-
7267
// Link the pending credential with the existing account
73-
await userCredential.user.linkWithCredential(pendingCredential);
74-
68+
await userCredential.user!.linkWithCredential(pendingCredential);
7569
// Success! Go back to your application flow
7670
return goToApplication();
71+
} on FirebaseAuthException catch (_) {
72+
// Email/password sign-in failed, try another provider
7773
}
7874
79-
// Since other providers are now external, you must now sign the user in with another
80-
// auth provider, such as Facebook.
81-
if (userSignInMethods.first == 'facebook.com') {
82-
// Create a new Facebook credential
83-
String accessToken = await triggerFacebookAuthentication();
84-
var facebookAuthCredential = FacebookAuthProvider.credential(accessToken);
85-
86-
// Sign the user in with the credential
87-
UserCredential userCredential = await auth.signInWithCredential(facebookAuthCredential);
75+
// Try signing in with Facebook if applicable
76+
String accessToken = await triggerFacebookAuthentication();
77+
var facebookAuthCredential =
78+
FacebookAuthProvider.credential(accessToken);
8879
89-
// Link the pending credential with the existing account
90-
await userCredential.user.linkWithCredential(pendingCredential);
80+
UserCredential userCredential =
81+
await auth.signInWithCredential(facebookAuthCredential);
9182
92-
// Success! Go back to your application flow
93-
return goToApplication();
94-
}
83+
// Link the pending credential with the existing account
84+
await userCredential.user!.linkWithCredential(pendingCredential);
9585
96-
// Handle other OAuth providers...
86+
// Success! Go back to your application flow
87+
return goToApplication();
9788
}
9889
}
9990
```

0 commit comments

Comments
 (0)