Skip to content

Commit 572a693

Browse files
refactor: remove duplicate code for phone sign-in
1 parent e7ec409 commit 572a693

5 files changed

Lines changed: 11 additions & 63 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,6 @@ public protocol AuthProviderUI {
3232
var provider: AuthProviderSwift { get }
3333
}
3434

35-
/// Protocol for phone authentication which requires a two-step flow:
36-
/// 1. Verify phone number to get verification ID
37-
/// 2. Create credential with verification ID and code
38-
public protocol PhoneAuthProviderSwift: AuthProviderSwift {
39-
@MainActor func verifyPhoneNumber(phoneNumber: String) async throws -> String
40-
@MainActor func createAuthCredential(verificationId: String,
41-
verificationCode: String) async throws -> AuthCredential
42-
}
43-
4435
public enum AuthenticationState {
4536
case unauthenticated
4637
case authenticating
@@ -172,10 +163,6 @@ public final class AuthService {
172163

173164
private var providers: [AuthProviderUI] = []
174165

175-
public var currentPhoneProvider: PhoneAuthProviderSwift? {
176-
providers.compactMap { $0.provider as? PhoneAuthProviderSwift }.first
177-
}
178-
179166
public func registerProvider(providerWithButton: AuthProviderUI) {
180167
providers.append(providerWithButton)
181168
}
@@ -209,11 +196,6 @@ public final class AuthService {
209196
}
210197
}
211198

212-
public func signIn(_: PhoneAuthProviderSwift) async {
213-
// Phone auth is multi-step, navigate to phone number entry
214-
navigator.push(.enterPhoneNumber)
215-
}
216-
217199
// MARK: - End Provider APIs
218200

219201
private func safeActionCodeSettings() throws -> ActionCodeSettings {

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EnterPhoneNumberView.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ struct EnterPhoneNumberView: View {
4848
Button(action: {
4949
Task {
5050
do {
51-
guard let provider = authService.currentPhoneProvider else {
52-
fatalError("No phone provider found")
53-
}
5451
let fullPhoneNumber = selectedCountry.dialCode + phoneNumber
55-
let id = try await provider.verifyPhoneNumber(phoneNumber: fullPhoneNumber)
52+
let id = try await authService.verifyPhoneNumber(phoneNumber: fullPhoneNumber)
5653
authService.navigator.push(.enterVerificationCode(
5754
verificationID: id,
5855
fullPhoneNumber: fullPhoneNumber

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EnterVerificationCodeView.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,10 @@ struct EnterVerificationCodeView: View {
5252
Button(action: {
5353
Task {
5454
do {
55-
guard let provider = authService.currentPhoneProvider else {
56-
fatalError("No phone provider found")
57-
}
58-
let credential = try await provider.createAuthCredential(
59-
verificationId: verificationID,
55+
try await authService.signInWithPhoneNumber(
56+
verificationID: verificationID,
6057
verificationCode: verificationCode
6158
)
62-
63-
_ = try await authService.signIn(credentials: credential)
6459
authService.navigator.clear()
6560
} catch {}
6661
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderAuthUI.swift

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,24 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
@preconcurrency import FirebaseAuth
1615
import FirebaseAuthSwiftUI
1716
import SwiftUI
1817

19-
public typealias VerificationID = String
20-
21-
public class PhoneProviderSwift: PhoneAuthProviderSwift {
18+
/// Simple provider marker for phone authentication
19+
public class PhoneProviderSwift: AuthProviderSwift {
2220
public init() {}
23-
24-
@MainActor public func verifyPhoneNumber(phoneNumber: String) async throws -> VerificationID {
25-
return try await withCheckedThrowingContinuation { continuation in
26-
PhoneAuthProvider.provider()
27-
.verifyPhoneNumber(phoneNumber, uiDelegate: nil) { verificationID, error in
28-
if let error = error {
29-
continuation.resume(throwing: error)
30-
return
31-
}
32-
continuation.resume(returning: verificationID!)
33-
}
34-
}
35-
}
36-
37-
@MainActor public func createAuthCredential(verificationId: String,
38-
verificationCode: String) async throws
39-
-> AuthCredential {
40-
return PhoneAuthProvider.provider()
41-
.credential(withVerificationID: verificationId, verificationCode: verificationCode)
42-
}
4321
}
4422

4523
public class PhoneAuthProviderAuthUI: AuthProviderUI {
46-
private let typedProvider: PhoneAuthProviderSwift
24+
private let typedProvider: PhoneProviderSwift
4725
public var provider: AuthProviderSwift { typedProvider }
4826
public let id: String = "phone"
4927

50-
public init(provider: PhoneAuthProviderSwift? = nil) {
51-
typedProvider = provider ?? PhoneProviderSwift()
28+
public init() {
29+
typedProvider = PhoneProviderSwift()
5230
}
5331

5432
@MainActor public func authButton() -> AnyView {
55-
AnyView(PhoneAuthButtonView(phoneProvider: typedProvider))
33+
AnyView(PhoneAuthButtonView())
5634
}
5735
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Views/PhoneAuthButtonView.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ import SwiftUI
2020
@MainActor
2121
public struct PhoneAuthButtonView {
2222
@Environment(AuthService.self) private var authService
23-
let phoneProvider: PhoneAuthProviderSwift
2423

25-
public init(phoneProvider: PhoneAuthProviderSwift) {
26-
self.phoneProvider = phoneProvider
27-
}
24+
public init() {}
2825
}
2926

3027
extension PhoneAuthButtonView: View {
@@ -41,7 +38,6 @@ extension PhoneAuthButtonView: View {
4138

4239
#Preview {
4340
FirebaseOptions.dummyConfigurationForPreview()
44-
let phoneProvider = PhoneProviderSwift()
45-
return PhoneAuthButtonView(phoneProvider: phoneProvider)
41+
return PhoneAuthButtonView()
4642
.environment(AuthService())
4743
}

0 commit comments

Comments
 (0)