Skip to content

Commit d31cc2a

Browse files
authored
Merge pull request firebase#522 from morganchen12/master
Update auth docs with anonymous login instructions
2 parents 06344ec + 98a7993 commit d31cc2a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

FirebaseAuthUI/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,52 @@ parent classes. For example:
319319

320320
Refer to the Objective-C and Swift samples for examples of how you can customize
321321
these views.
322+
323+
## Handling auto-upgrade of anonymous users
324+
325+
Enabling auto-upgrade of anonymous users increases the complexity of your auth
326+
flow by adding several more edge cases that need to be handled. As opposed to
327+
normal auth, which only involves one step, auto-upgrade presents three steps
328+
with four possibilities total:
329+
- At app launch, anonymously authenticate the user. User state can be
330+
accumulated on the anonymous user and linked to the non-anonymous account
331+
later.
332+
- At some point in your app, present the auth flow and authenticate the user
333+
using a non-anonymous auth method.
334+
- Following a successful auth attempt, if the user signs in to a new account,
335+
the anonymous account and the new account can be linked together without
336+
issue.
337+
- Otherwise, if logging into an existing user, FirebaseUI will return a merge
338+
conflict error containing the resulting `FIRAuthDataResult` corresponding to
339+
the existing account. This value should be used to login to the existing
340+
account without linking to the anonymous user, as the two accounts may have
341+
conflicting state (the anonymous account state will be discarded).
342+
343+
```swift
344+
func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) {
345+
if let error = error as NSError?,
346+
error.code == FUIAuthErrorCode.mergeConflict.rawValue {
347+
// Merge conflict error, discard the anonymous user and login as the existing
348+
// non-anonymous user.
349+
guard let credential = error.userInfo[FUIAuthCredentialKey] as? AuthCredential else {
350+
print("Received merge conflict error without auth credential!")
351+
return
352+
}
353+
354+
Auth.auth().signInAndRetrieveData(with: credential) { (dataResult, error) in
355+
if let error = error as NSError? {
356+
print("Failed to re-login: \(error)")
357+
return
358+
}
359+
360+
// Handle successful login
361+
}
362+
} else if let error = error {
363+
// Some non-merge conflict error happened.
364+
print("Failed to log in: \(error)")
365+
return
366+
}
367+
368+
// Handle successful login
369+
}
370+
```

0 commit comments

Comments
 (0)