Skip to content

Commit

Permalink
Change Custom Flow Swift Code Blocks to 2 Spaces (#1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepitre authored Aug 1, 2024
1 parent d421c5e commit e604f71
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 522 deletions.
113 changes: 56 additions & 57 deletions docs/custom-flows/add-email.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -161,72 +161,71 @@ The following example demonstrates how to build a custom user interface that all
import ClerkSDK

struct AddEmailView: View {
@State private var email = ""
@State private var code = ""
@State private var isVerifying = false
// Create a reference to the email address that we'll be creating
@State private var newEmailAddress: EmailAddress?
@State private var email = ""
@State private var code = ""
@State private var isVerifying = false
// Create a reference to the email address that we'll be creating
@State private var newEmailAddress: EmailAddress?

var body: some View {
if newEmailAddress?.verification?.status == .verified {
Text("Email added!")
}

var body: some View {

if newEmailAddress?.verification?.status == .verified {
Text("Email added!")
}

if isVerifying {
TextField("Enter code", text: $code)
Button("Verify") {
Task { await verifyCode(code) }
}
} else {
TextField("Enter email address", text: $email)
Button("Continue") {
Task { await createEmail(email) }
}
}
if isVerifying {
TextField("Enter code", text: $code)
Button("Verify") {
Task { await verifyCode(code) }
}
} else {
TextField("Enter email address", text: $email)
Button("Continue") {
Task { await createEmail(email) }
}
}
}
}

extension AddEmailView {

func createEmail(_ email: String) async {
do {
guard let user = Clerk.shared.user else { return }

// Add an unverified email address to user
self.newEmailAddress = try await user.createEmailAddress(email)

guard let newEmailAddress = self.newEmailAddress else { return }

// Send the user an email with the verification code
try await newEmailAddress.prepareVerification(strategy: .emailCode)

// Set to true to display second form
// and capture the OTP code
isVerifying = true
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
func createEmail(_ email: String) async {
do {
guard let user = Clerk.shared.user else { return }

// Add an unverified email address to user
self.newEmailAddress = try await user.createEmailAddress(email)

guard let newEmailAddress = self.newEmailAddress else { return }

// Send the user an email with the verification code
try await newEmailAddress.prepareVerification(strategy: .emailCode)

// Set to true to display second form
// and capture the OTP code
isVerifying = true
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
}

func verifyCode(_ code: String) async {
do {
guard let newEmailAddress else { return }

// Verify that the code entered matches the code sent to the user
self.newEmailAddress = try await newEmailAddress.attemptVerification(strategy: .emailCode(code: code))

// If the status is not complete, check why. User may need to
// complete further steps.
dump(self.newEmailAddress?.verification?.status)
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
func verifyCode(_ code: String) async {
do {
guard let newEmailAddress else { return }

// Verify that the code entered matches the code sent to the user
self.newEmailAddress = try await newEmailAddress.attemptVerification(strategy: .emailCode(code: code))

// If the status is not complete, check why. User may need to
// complete further steps.
dump(self.newEmailAddress?.verification?.status)
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
}
}
```
</Tab>
Expand Down
115 changes: 57 additions & 58 deletions docs/custom-flows/add-phone.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -160,72 +160,71 @@ The following example demonstrates how to build a custom user interface that all
import ClerkSDK

struct AddPhoneView: View {
@State private var phone = ""
@State private var code = ""
@State private var isVerifying = false
// Create a reference to the phone number that we'll be creating
@State private var newPhoneNumber: PhoneNumber?

var body: some View {

if newPhoneNumber?.verification?.status == .verified {
Text("Phone added!")
}

if isVerifying {
TextField("Enter code", text: $code)
Button("Verify") {
Task { await verifyCode(code) }
}
} else {
TextField("Enter phone number", text: $phone)
Button("Continue") {
Task { await createPhone(phone) }
}
}
@State private var phone = ""
@State private var code = ""
@State private var isVerifying = false
// Create a reference to the phone number that we'll be creating
@State private var newPhoneNumber: PhoneNumber?

var body: some View {
if newPhoneNumber?.verification?.status == .verified {
Text("Phone added!")
}

if isVerifying {
TextField("Enter code", text: $code)
Button("Verify") {
Task { await verifyCode(code) }
}
} else {
TextField("Enter phone number", text: $phone)
Button("Continue") {
Task { await createPhone(phone) }
}
}
}
}

extension AddPhoneView {

func createPhone(_ phone: String) async {
do {
guard let user = Clerk.shared.user else { return }

// Add an unverified phone number to user
self.newPhoneNumber = try await user.createPhoneNumber(phone)

guard let newphoneNumber = self.newPhoneNumber else { return }

// Send the user an sms message with the verification code
try await newphoneNumber.prepareVerification()

// Set to true to display second form
// and capture the OTP code
isVerifying = true
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
func createPhone(_ phone: String) async {
do {
guard let user = Clerk.shared.user else { return }

// Add an unverified phone number to user
self.newPhoneNumber = try await user.createPhoneNumber(phone)

guard let newphoneNumber = self.newPhoneNumber else { return }

// Send the user an sms message with the verification code
try await newphoneNumber.prepareVerification()

// Set to true to display second form
// and capture the OTP code
isVerifying = true
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
}

func verifyCode(_ code: String) async {
do {
guard let newPhoneNumber else { return }

// Verify that the code entered matches the code sent to the user
self.newPhoneNumber = try await newPhoneNumber.attemptVerification(code: code)

// If the status is not complete, check why. User may need to
// complete further steps.
dump(self.newPhoneNumber?.verification?.status)
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
func verifyCode(_ code: String) async {
do {
guard let newPhoneNumber else { return }

// Verify that the code entered matches the code sent to the user
self.newPhoneNumber = try await newPhoneNumber.attemptVerification(code: code)

// If the status is not complete, check why. User may need to
// complete further steps.
dump(self.newPhoneNumber?.verification?.status)
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
}
}
```
</Tab>
Expand Down
118 changes: 59 additions & 59 deletions docs/custom-flows/email-password-mfa.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -579,73 +579,73 @@ This guide will walk you through how to build a custom email/password sign-in fl
import ClerkSDK

struct MFASignInView: View {
@State private var email = ""
@State private var password = ""
@State private var code = ""
@State private var displayTOTP = false

var body: some View {
if displayTOTP {
TextField("Code", text: $code)
Button("Verify") {
Task { await verify(code: code) }
}
} else {
TextField("Email", text: $email)
SecureField("Password", text: $password)
Button("Next") {
Task { await submit(email: email, password: password) }
}
}
@State private var email = ""
@State private var password = ""
@State private var code = ""
@State private var displayTOTP = false

var body: some View {
if displayTOTP {
TextField("Code", text: $code)
Button("Verify") {
Task { await verify(code: code) }
}
} else {
TextField("Email", text: $email)
SecureField("Password", text: $password)
Button("Next") {
Task { await submit(email: email, password: password) }
}
}
}
}

extension MFASignInView {

func submit(email: String, password: String) async {
do {
// Start the sign-in process.
let signIn = try await SignIn.create(strategy: .identifier(email, password: password))

switch signIn.status {
case .needsSecondFactor:
// Handle user submitting email and password and swapping to TOTP form.
displayTOTP = true
default:
// If the status is not needsSecondFactor, check why. User may need to
// complete different steps.
dump(signIn.status)
}
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
func submit(email: String, password: String) async {
do {
// Start the sign-in process.
let signIn = try await SignIn.create(strategy: .identifier(email, password: password))

switch signIn.status {
case .needsSecondFactor:
// Handle user submitting email and password and swapping to TOTP form.
displayTOTP = true
default:
// If the status is not needsSecondFactor, check why. User may need to
// complete different steps.
dump(signIn.status)
}
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
}

func verify(code: String) async {
do {
// Access the in progress sign in stored on the client object.
guard let inProgressSignIn = Clerk.shared.client?.signIn else { return }

// Attempt the TOTP or backup code verification.
let signIn = try await inProgressSignIn.attemptSecondFactor(for: .totp(code: code))

switch signIn.status {
case .complete:
// If sign-in process is complete, navigate the user as needed.
dump(Clerk.shared.session)
default:
// If the status is not complete, check why. User may need to
// complete further steps.
dump(signIn.status)
}
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
func verify(code: String) async {
do {
// Access the in progress sign in stored on the client object.
guard let inProgressSignIn = Clerk.shared.client?.signIn else { return }

// Attempt the TOTP or backup code verification.
let signIn = try await inProgressSignIn.attemptSecondFactor(for: .totp(code: code))

switch signIn.status {
case .complete:
// If sign-in process is complete, navigate the user as needed.
dump(Clerk.shared.session)
default:
// If the status is not complete, check why. User may need to
// complete further steps.
dump(signIn.status)
}
} catch {
// See https://clerk.com/docs/custom-flows/error-handling
// for more info on error handling
dump(error)
}
}
}
```
</Tab>
Expand Down
Loading

0 comments on commit e604f71

Please sign in to comment.