Skip to content

Commit ea5fa12

Browse files
authored
update ios custom flows code examples (#2004)
1 parent 4a45e9f commit ea5fa12

8 files changed

+45
-67
lines changed

docs/custom-flows/add-email.mdx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ The following example demonstrates how to build a custom user interface that all
151151
<Tab>
152152
```swift {{ filename: 'AddEmailView.swift', collapsible: true }}
153153
import SwiftUI
154-
import ClerkSDK
154+
import Clerk
155155

156156
struct AddEmailView: View {
157157
@State private var email = ""
@@ -185,13 +185,11 @@ The following example demonstrates how to build a custom user interface that all
185185
do {
186186
guard let user = Clerk.shared.user else { return }
187187

188-
// Add an unverified email address to user
189-
self.newEmailAddress = try await user.createEmailAddress(email)
190-
191-
guard let newEmailAddress = self.newEmailAddress else { return }
192-
193-
// Send the user an email with the verification code
194-
try await newEmailAddress.prepareVerification(strategy: .emailCode)
188+
// Add an unverified email address to user,
189+
// then send the user an email with the verification code
190+
self.newEmailAddress = try await user
191+
.createEmailAddress(email)
192+
.prepareVerification(strategy: .emailCode)
195193

196194
// Set to true to display second form
197195
// and capture the OTP code

docs/custom-flows/add-phone.mdx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ The following example demonstrates how to build a custom user interface that all
152152
<Tab>
153153
```swift {{ filename: 'AddPhoneView.swift', collapsible: true }}
154154
import SwiftUI
155-
import ClerkSDK
155+
import Clerk
156156

157157
struct AddPhoneView: View {
158158
@State private var phone = ""
@@ -186,13 +186,11 @@ The following example demonstrates how to build a custom user interface that all
186186
do {
187187
guard let user = Clerk.shared.user else { return }
188188

189-
// Add an unverified phone number to user
190-
self.newPhoneNumber = try await user.createPhoneNumber(phone)
191-
192-
guard let newphoneNumber = self.newPhoneNumber else { return }
193-
194-
// Send the user an sms message with the verification code
195-
try await newphoneNumber.prepareVerification()
189+
// Add an unverified phone number to user,
190+
// then send the user an sms message with the verification code
191+
self.newPhoneNumber = try await user
192+
.createPhoneNumber(phone)
193+
.prepareVerification()
196194

197195
// Set to true to display second form
198196
// and capture the OTP code

docs/custom-flows/email-password-mfa.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ This guide will walk you through how to build a custom email/password sign-in fl
459459
<Tab>
460460
```swift {{ filename: 'MFASignInView.swift', collapsible: true }}
461461
import SwiftUI
462-
import ClerkSDK
462+
import Clerk
463463

464464
struct MFASignInView: View {
465465
@State private var email = ""

docs/custom-flows/email-password.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ This guide will walk you through how to build a custom email/password sign-up an
391391
<Tab>
392392
```swift {{ filename: 'EmailPasswordSignUpView.swift', collapsible: true }}
393393
import SwiftUI
394-
import ClerkSDK
394+
import Clerk
395395

396396
struct EmailPasswordSignUpView: View {
397397
@State private var email = ""
@@ -712,7 +712,7 @@ This guide will walk you through how to build a custom email/password sign-up an
712712
<Tab>
713713
```swift {{ filename: 'EmailPasswordSignInView.swift', collapsible: true }}
714714
import SwiftUI
715-
import ClerkSDK
715+
import Clerk
716716

717717
struct EmailPasswordSignInView: View {
718718
@State private var email = ""

docs/custom-flows/email-sms-otp.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ This guide will walk you through how to build a custom SMS OTP sign-up and sign-
234234
<Tab>
235235
```swift {{ filename: 'SMSOTPSignUpView.swift', collapsible: true }}
236236
import SwiftUI
237-
import ClerkSDK
237+
import Clerk
238238

239239
struct SMSOTPSignUpView: View {
240240
@State private var phoneNumber = ""
@@ -554,7 +554,7 @@ This guide will walk you through how to build a custom SMS OTP sign-up and sign-
554554
<Tab>
555555
```swift {{ filename: 'SMSOTPSignInView.swift', collapsible: true }}
556556
import SwiftUI
557-
import ClerkSDK
557+
import Clerk
558558

559559
struct SMSOTPSignInView: View {
560560
@State private var phoneNumber = ""
@@ -583,8 +583,18 @@ This guide will walk you through how to build a custom SMS OTP sign-up and sign-
583583
// Start the sign-in process using the phone number method.
584584
let signIn = try await SignIn.create(strategy: .identifier(phoneNumber))
585585

586+
// Find the phoneNumberId from all the available first factors for the current sign-in
587+
guard
588+
let phoneNumberFactor = signIn.supportedFirstFactors?.first(where: { factor in
589+
factor.strategy == "phone_code"
590+
}),
591+
let phoneNumberId = phoneNumberFactor.phoneNumberId
592+
else {
593+
return
594+
}
595+
586596
// Send the OTP code to the user.
587-
try await signIn.prepareFirstFactor(for: .phoneCode)
597+
try await signIn.prepareFirstFactor(for: .phoneCode(phoneNumberId: phoneNumberId))
588598

589599
// Set isVerifying to true to display second form
590600
// and capture the OTP code.

docs/custom-flows/forgot-password.mdx

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ In the following example, the user is asked to provide their email address. Afte
150150

151151
```swift {{ filename: 'ForgotPasswordView.swift', collapsible: true }}
152152
import SwiftUI
153-
import ClerkSDK
153+
import Clerk
154154

155155
struct ForgotPasswordView: View {
156-
@ObservedObject private var clerk = Clerk.shared
156+
@Environment(Clerk.self) private var clerk
157157
@State private var email = ""
158158
@State private var code = ""
159159
@State private var newPassword = ""
@@ -162,15 +162,9 @@ In the following example, the user is asked to provide their email address. Afte
162162
var body: some View {
163163
switch clerk.client?.signIn?.status {
164164
case .needsFirstFactor:
165-
if isVerifying {
166-
TextField("Enter your code", text: $code)
167-
Button("Verify") {
168-
Task { await verify(code: code) }
169-
}
170-
} else {
171-
Button("Forgot password?") {
172-
Task { await sendResetCode() }
173-
}
165+
TextField("Enter your code", text: $code)
166+
Button("Verify") {
167+
Task { await verify(code: code) }
174168
}
175169

176170
case .needsSecondFactor:
@@ -187,7 +181,7 @@ In the following example, the user is asked to provide their email address. Afte
187181
Text("Active Session: \(session.id)")
188182
} else {
189183
TextField("Email", text: $email)
190-
Button("Continue") {
184+
Button("Forgot password?") {
191185
Task { await createSignIn(email: email) }
192186
}
193187
}
@@ -199,25 +193,8 @@ In the following example, the user is asked to provide their email address. Afte
199193

200194
func createSignIn(email: String) async {
201195
do {
202-
// Start the sign in process
203-
try await SignIn.create(strategy: .identifier(email))
204-
} catch {
205-
// See https://clerk.com/docs/custom-flows/error-handling
206-
// for more info on error handling
207-
dump(error)
208-
}
209-
}
210-
211-
func sendResetCode() async {
212-
do {
213-
// Access the in progress sign in stored on the client object.
214-
guard let inProgressSignIn = clerk.client?.signIn else { return }
215-
216-
// Send the password reset code to the user's email
217-
try await inProgressSignIn.prepareFirstFactor(for: .resetPasswordEmailCode)
218-
219-
// Set isVerifying to true to capture the OTP code.
220-
isVerifying = true
196+
// Start the sign in reset password process
197+
try await SignIn.create(strategy: .identifier(email, strategy: "reset_password_email_code"))
221198
} catch {
222199
// See https://clerk.com/docs/custom-flows/error-handling
223200
// for more info on error handling

docs/custom-flows/oauth-connections.mdx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ You must configure your application instance through the Clerk Dashboard for the
7676
<Tab>
7777
```swift {{ filename: 'OAuthView.swift', collapsible: true }}
7878
import SwiftUI
79-
import ClerkSDK
79+
import Clerk
8080

8181
struct OAuthView: View {
8282
var body: some View {
@@ -93,18 +93,15 @@ You must configure your application instance through the Clerk Dashboard for the
9393
func signInWithOAuth(provider: OAuthProvider) async {
9494
do {
9595
// Start the sign-in process using the selected OAuth provider.
96-
let signIn = try await SignIn.create(strategy: .oauth(provider))
97-
98-
// Start the OAuth process
99-
let externalAuthResult = try await signIn.authenticateWithRedirect()
96+
let result = try await SignIn.authenticateWithRedirect(strategy: .oauth(provider: provider))
10097

10198
// It is common for users who are authenticating with OAuth to use
10299
// a sign-in button when they mean to sign-up, and vice versa.
103100
// Clerk will handle this transfer for you if possible.
104-
// Therefore, an ExternalAuthResult can contain either a SignIn or SignUp.
101+
// Therefore, a TransferFlowResult can be either a SignIn or SignUp.
105102

106-
// Check if the result of the OAuth was a sign in
107-
if let signIn = externalAuthResult?.signIn {
103+
switch result {
104+
case .signIn(let signIn):
108105
switch signIn.status {
109106
case .complete:
110107
// If sign-in process is complete, navigate the user as needed.
@@ -114,10 +111,7 @@ You must configure your application instance through the Clerk Dashboard for the
114111
// complete further steps.
115112
dump(signIn.status)
116113
}
117-
}
118-
119-
// Check if the result of the OAuth was a sign up
120-
if let signUp = externalAuthResult?.signUp {
114+
case .signUp(let signUp):
121115
switch signUp.status {
122116
case .complete:
123117
// If sign-up process is complete, navigate the user as needed.
@@ -128,6 +122,7 @@ You must configure your application instance through the Clerk Dashboard for the
128122
dump(signUp.status)
129123
}
130124
}
125+
131126
} catch {
132127
// See https://clerk.com/docs/custom-flows/error-handling
133128
// for more info on error handling.

docs/custom-flows/sign-out.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ The `signOut()` function signs a user out of all sessions in a [multi-session ap
106106
<Tab>
107107
```swift {{ filename: 'SignOutView.swift', collapsible: true }}
108108
import SwiftUI
109-
import ClerkSDK
109+
import Clerk
110110

111111
struct SignOutView: View {
112-
@ObservedObject private var clerk = Clerk.shared
112+
@Environment(Clerk.self) private var clerk
113113

114114
var body: some View {
115115
if let session = clerk.session {

0 commit comments

Comments
 (0)