|
1 | | -Project: /docs/_project.yaml |
2 | | -Book: /docs/_book.yaml |
| 1 | +Project: /docs/\_project.yaml |
| 2 | +Book: /docs/\_book.yaml |
3 | 3 |
|
4 | 4 | <link rel="stylesheet" type="text/css" href="/styles/docs.css" /> |
5 | 5 |
|
@@ -51,49 +51,40 @@ try { |
51 | 51 | } on FirebaseAuthException catch (e) { |
52 | 52 | if (e.code == 'account-exists-with-different-credential') { |
53 | 53 | // 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!; |
56 | 56 |
|
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. |
59 | 60 |
|
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 { |
67 | 63 | UserCredential userCredential = await auth.signInWithEmailAndPassword( |
68 | 64 | email: email, |
69 | | - password: password, |
| 65 | + password: promptUserForPassword(), // prompt user for password |
70 | 66 | ); |
71 | | -
|
72 | 67 | // Link the pending credential with the existing account |
73 | | - await userCredential.user.linkWithCredential(pendingCredential); |
74 | | -
|
| 68 | + await userCredential.user!.linkWithCredential(pendingCredential); |
75 | 69 | // Success! Go back to your application flow |
76 | 70 | return goToApplication(); |
| 71 | + } on FirebaseAuthException catch (_) { |
| 72 | + // Email/password sign-in failed, try another provider |
77 | 73 | } |
78 | 74 |
|
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); |
88 | 79 |
|
89 | | - // Link the pending credential with the existing account |
90 | | - await userCredential.user.linkWithCredential(pendingCredential); |
| 80 | + UserCredential userCredential = |
| 81 | + await auth.signInWithCredential(facebookAuthCredential); |
91 | 82 |
|
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); |
95 | 85 |
|
96 | | - // Handle other OAuth providers... |
| 86 | + // Success! Go back to your application flow |
| 87 | + return goToApplication(); |
97 | 88 | } |
98 | 89 | } |
99 | 90 | ``` |
|
0 commit comments