Skip to content

Commit 4341794

Browse files
improve phone auth flow
1 parent 6b96797 commit 4341794

3 files changed

Lines changed: 62 additions & 45 deletions

File tree

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ public class PhoneAuthProviderSwift: @preconcurrency PhoneAuthProviderProtocol {
1818
}
1919
}
2020
}
21-
}
21+
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Views/PhoneAuthButtonView.swift

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ extension PhoneAuthButtonView: View {
1717
public var body: some View {
1818
if authService.authenticationState != .authenticating {
1919
VStack {
20-
TextField("Enter phone number", text: $phoneNumber)
21-
.keyboardType(.phonePad)
22-
.padding()
23-
.background(Color(.systemGray6))
24-
.cornerRadius(8)
25-
.padding(.horizontal)
26-
20+
LabeledContent {
21+
TextField("Enter phone number", text: $phoneNumber)
22+
.textInputAutocapitalization(.never)
23+
.disableAutocorrection(true)
24+
.submitLabel(.next)
25+
} label: {
26+
Image(systemName: "at")
27+
}.padding(.vertical, 6)
28+
.background(Divider(), alignment: .bottom)
29+
.padding(.bottom, 4)
2730
Button(action: {
2831
Task {
2932
do {
@@ -37,46 +40,46 @@ extension PhoneAuthButtonView: View {
3740
}
3841
}
3942
}) {
40-
Text("Send Verification Code")
43+
Text("Send SMS code")
44+
.padding(.vertical, 8)
45+
.frame(maxWidth: .infinity)
46+
}
47+
.disabled(!PhoneUtils.isValidPhoneNumber(phoneNumber))
48+
.padding([.top, .bottom], 8)
49+
.frame(maxWidth: .infinity)
50+
.buttonStyle(.borderedProminent)
51+
Text(errorMessage).foregroundColor(.red)
52+
}.sheet(isPresented: $showVerificationCodeInput) {
53+
TextField("Enter verification code", text: $verificationCode)
54+
.keyboardType(.numberPad)
55+
.padding()
56+
.background(Color(.systemGray6))
57+
.cornerRadius(8)
58+
.padding(.horizontal)
59+
60+
Button(action: {
61+
Task {
62+
do {
63+
try await authService.signInWithPhoneNumber(
64+
verificationID: verificationID,
65+
verificationCode: verificationCode
66+
)
67+
} catch {
68+
errorMessage = authService.string.localizedErrorMessage(for: error)
69+
}
70+
showVerificationCodeInput = false
71+
}
72+
}) {
73+
Text("Verify phone number and sign-in")
4174
.foregroundColor(.white)
4275
.padding()
4376
.frame(maxWidth: .infinity)
44-
.background(Color.blue)
77+
.background(Color.green)
4578
.cornerRadius(8)
4679
.padding(.horizontal)
47-
}.disabled(!PhoneUtils.isValidPhoneNumber(phoneNumber))
48-
}
49-
.sheet(isPresented: $showVerificationCodeInput) {
50-
VStack {
51-
TextField("Enter verification code", text: $verificationCode)
52-
.keyboardType(.numberPad)
53-
.padding()
54-
.background(Color(.systemGray6))
55-
.cornerRadius(8)
56-
.padding(.horizontal)
57-
58-
Button(action: {
59-
Task {
60-
do {
61-
try await authService.signInWithPhoneNumber(
62-
verificationID: verificationID,
63-
verificationCode: verificationCode
64-
)
65-
} catch {
66-
errorMessage = authService.string.localizedErrorMessage(for: error)
67-
}
68-
showVerificationCodeInput = false
69-
}
70-
}) {
71-
Text("Verify and Sign In")
72-
.foregroundColor(.white)
73-
.padding()
74-
.frame(maxWidth: .infinity)
75-
.background(Color.green)
76-
.cornerRadius(8)
77-
.padding(.horizontal)
78-
}
7980
}
81+
}.onOpenURL { url in
82+
authService.auth.canHandle(url)
8083
}
8184
} else {
8285
ProgressView()

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,28 @@ class AppDelegate: NSObject, UIApplicationDelegate {
2222
didFinishLaunchingWithOptions launchOptions: [
2323
UIApplication.LaunchOptionsKey: Any
2424
]?) -> Bool {
25+
FirebaseApp.configure()
2526
ApplicationDelegate.shared.application(
2627
application,
2728
didFinishLaunchingWithOptions: launchOptions
2829
)
2930
return true
3031
}
3132

33+
func application(_: UIApplication,
34+
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
35+
Auth.auth().setAPNSToken(deviceToken, type: .prod)
36+
}
37+
38+
func application(_: UIApplication, didReceiveRemoteNotification notification: [AnyHashable: Any],
39+
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)
40+
-> Void) {
41+
if Auth.auth().canHandleNotification(notification) {
42+
completionHandler(.noData)
43+
return
44+
}
45+
}
46+
3247
func application(_ app: UIApplication,
3348
open url: URL,
3449
options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
@@ -39,6 +54,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
3954
.sourceApplication] as? String,
4055
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
4156
)
57+
4258
return googleProvider.handleUrl(url)
4359
}
4460
}
@@ -47,9 +63,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
4763
struct FirebaseSwiftUIExampleApp: App {
4864
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
4965

50-
init() {
51-
FirebaseApp.configure()
52-
}
66+
init() {}
5367

5468
var body: some Scene {
5569
WindowGroup {

0 commit comments

Comments
 (0)