Skip to content

Commit 1cb5bec

Browse files
docs: update documentation for email link reauth
1 parent 0d3bde9 commit 1cb5bec

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

FirebaseSwiftUI/README.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ When a sensitive operation requires reauthentication, the default views automati
278278

279279
- **Email/Password**: Present a sheet prompting the user to enter their password before continuing.
280280

281+
- **Email Link**: Show an alert asking to send a verification email, then present a sheet with instructions to check email. The user taps the link in their email to complete reauthentication.
282+
281283
- **Phone**: Show an alert explaining verification is needed, then present a sheet for SMS code verification.
282284

283285
The operation automatically retries after successful reauthentication. No additional code is required when using `AuthPickerView` or the built-in account management views (`UpdatePasswordView`, `SignedInView`, etc.).
@@ -655,7 +657,7 @@ When building custom views, you need to handle several things yourself that `Aut
655657

656658
### Reauthentication in Custom Views
657659

658-
When building custom views, handle reauthentication by catching specific errors and implementing your own flow. Sensitive operations throw three types of reauthentication errors, each containing context information.
660+
When building custom views, handle reauthentication by catching specific errors and implementing your own flow. Sensitive operations throw four types of reauthentication errors, each containing context information.
659661

660662
#### Implementation Patterns
661663

@@ -720,6 +722,31 @@ do {
720722
}
721723
```
722724

725+
**Email Link:**
726+
727+
Catch the error, send verification email, and handle the incoming URL:
728+
729+
```swift
730+
do {
731+
try await authService.updatePassword(to: newPassword)
732+
} catch let error as AuthServiceError {
733+
if case .emailLinkReauthenticationRequired(let context) = error {
734+
// Send verification email
735+
try await authService.sendEmailSignInLink(
736+
email: context.email,
737+
isReauth: true
738+
)
739+
// Show your "Check your email" UI
740+
await showCheckEmailUI()
741+
// When user taps the link, it opens your app with a URL
742+
// Handle it in your URL handler:
743+
// try await authService.handleSignInLink(url: url)
744+
// The handleSignInLink method automatically completes reauthentication
745+
try await authService.updatePassword(to: newPassword) // Retry
746+
}
747+
}
748+
```
749+
723750
All reauthentication context objects include a `.displayMessage` property for user-facing text.
724751

725752
### Custom OAuth Providers
@@ -1193,18 +1220,21 @@ Links a new authentication method to the current user's account.
11931220
##### Send Email Sign-In Link
11941221

11951222
```swift
1196-
public func sendEmailSignInLink(email: String) async throws
1223+
public func sendEmailSignInLink(email: String, isReauth: Bool = false) async throws
11971224
```
11981225

1199-
Sends a sign-in link to the specified email address.
1226+
Sends a sign-in link to the specified email address. Can also be used for reauthentication.
12001227

12011228
**Parameters:**
12021229
- `email`: Email address to send the link to
1230+
- `isReauth`: Whether this is for reauthentication (default: `false`)
12031231

12041232
**Throws:** `AuthServiceError` or Firebase Auth errors
12051233

12061234
**Requirements:** `emailLinkSignInActionCodeSettings` must be configured in `AuthConfiguration`
12071235

1236+
**Note:** When `isReauth` is `true`, the method stores the email for reauthentication flow. The same `handleSignInLink(url:)` method handles both sign-in and reauthentication automatically.
1237+
12081238
---
12091239

12101240
##### Handle Sign-In Link
@@ -1213,13 +1243,15 @@ Sends a sign-in link to the specified email address.
12131243
public func handleSignInLink(url url: URL) async throws
12141244
```
12151245

1216-
Handles the sign-in flow when the user taps the email link.
1246+
Handles the email link flow when the user taps the link. Automatically routes to either sign-in or reauthentication based on the current context.
12171247

12181248
**Parameters:**
12191249
- `url`: The deep link URL from the email
12201250

12211251
**Throws:** `AuthServiceError` or Firebase Auth errors
12221252

1253+
**Note:** This method handles both initial sign-in and reauthentication flows automatically. The behavior is determined by whether `sendEmailSignInLink(email:isReauth:)` was called with `isReauth: true`.
1254+
12231255
---
12241256

12251257
#### Phone Authentication
@@ -1305,7 +1337,7 @@ Updates the current user's password. This is a sensitive operation that may requ
13051337

13061338
**Throws:**
13071339
- `AuthServiceError.noCurrentUser` if no user is signed in
1308-
- Reauthentication errors (`emailReauthenticationRequired`, `phoneReauthenticationRequired`, or `oauthReauthenticationRequired`) if recent authentication is required - see [Reauthentication](#reauthentication-in-default-views)
1340+
- Reauthentication errors (`emailReauthenticationRequired`, `emailLinkReauthenticationRequired`, `phoneReauthenticationRequired`, or `oauthReauthenticationRequired`) if recent authentication is required - see [Reauthentication](#reauthentication-in-default-views)
13091341
- Firebase Auth errors
13101342

13111343
---
@@ -1332,7 +1364,7 @@ Deletes the current user's account. This is a sensitive operation that requires
13321364

13331365
**Throws:**
13341366
- `AuthServiceError.noCurrentUser` if no user is signed in
1335-
- Reauthentication errors (`emailReauthenticationRequired`, `phoneReauthenticationRequired`, or `oauthReauthenticationRequired`) if recent authentication is required - see [Reauthentication](#reauthentication-in-default-views)
1367+
- Reauthentication errors (`emailReauthenticationRequired`, `emailLinkReauthenticationRequired`, `phoneReauthenticationRequired`, or `oauthReauthenticationRequired`) if recent authentication is required - see [Reauthentication](#reauthentication-in-default-views)
13361368
- Firebase Auth errors
13371369

13381370
---
@@ -1638,6 +1670,7 @@ public enum AuthServiceError: Error {
16381670
case multiFactorAuth(String)
16391671
case oauthReauthenticationRequired(context: OAuthReauthContext)
16401672
case emailReauthenticationRequired(context: EmailReauthContext)
1673+
case emailLinkReauthenticationRequired(context: EmailLinkReauthContext)
16411674
case phoneReauthenticationRequired(context: PhoneReauthContext)
16421675
case accountConflict(AccountConflictContext)
16431676
}
@@ -1653,6 +1686,8 @@ Thrown by sensitive operations when Firebase requires recent authentication. Eac
16531686

16541687
- **`emailReauthenticationRequired(context: EmailReauthContext)`**: Email/password provider. Context contains `email` and `displayMessage`. Prompt for password, then call `reauthenticate(with:)`.
16551688

1689+
- **`emailLinkReauthenticationRequired(context: EmailLinkReauthContext)`**: Email link (passwordless) provider. Context contains `email` and `displayMessage`. Send verification email with `sendEmailSignInLink(email:isReauth:true)`, then handle the incoming link with `handleSignInLink(url:)`.
1690+
16561691
- **`phoneReauthenticationRequired(context: PhoneReauthContext)`**: Phone provider. Context contains `phoneNumber` and `displayMessage`. Handle SMS verification, then call `reauthenticate(with:)`.
16571692

16581693
---

0 commit comments

Comments
 (0)