From fafaf88ff82faaab80a7fb5c34f2574b864031b9 Mon Sep 17 00:00:00 2001 From: Justin Leger Date: Sun, 5 May 2024 11:44:51 -0500 Subject: [PATCH 1/2] Added support to initialize with a specific FirebaseApp instance. --- .../Core/AuthManager.swift | 7 +++++- .../Providers/FirebaseAuthProvider.swift | 25 +++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift b/Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift index 50dc9f4..1477552 100644 --- a/Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift +++ b/Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift @@ -6,6 +6,7 @@ // import Foundation +import FirebaseAuth public struct AuthInfo { public let profile: UserAuthInfo? @@ -20,12 +21,16 @@ public struct AuthInfo { } public enum Configuration { - case mock, firebase + case mock + case firebase + case firebaseAuth(auth: Auth) var provider: AuthProvider { switch self { case .firebase: return FirebaseAuthProvider() + case .firebaseAuth(let auth): + return FirebaseAuthProvider(firebaseAuth: auth) case .mock: return MockAuthProvider() } diff --git a/Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift b/Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift index 8b7fc8b..9a531b8 100644 --- a/Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift +++ b/Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift @@ -6,12 +6,23 @@ // import Foundation +import Firebase import FirebaseAuth struct FirebaseAuthProvider: AuthProvider { + private var auth: Auth + + init() { + self.auth = Auth.auth() + } + + init(firebaseAuth: Auth) { + self.auth = firebaseAuth + } + func getAuthenticatedUser() -> UserAuthInfo? { - if let currentUser = Auth.auth().currentUser { + if let currentUser = auth.currentUser { return UserAuthInfo(user: currentUser) } else { return nil @@ -21,7 +32,7 @@ struct FirebaseAuthProvider: AuthProvider { @MainActor func authenticationDidChangeStream() -> AsyncStream { AsyncStream { continuation in - Auth.auth().addStateDidChangeListener { _, currentUser in + auth.addStateDidChangeListener { _, currentUser in if let currentUser { let user = UserAuthInfo(user: currentUser) continuation.yield(user) @@ -146,11 +157,11 @@ struct FirebaseAuthProvider: AuthProvider { } func signOut() throws { - try Auth.auth().signOut() + try auth.signOut() } func deleteAccount() async throws { - guard let user = Auth.auth().currentUser else { + guard let user = auth.currentUser else { throw AuthError.userNotFound } @@ -161,11 +172,11 @@ struct FirebaseAuthProvider: AuthProvider { private func signIn(credential: AuthCredential) async throws -> AuthDataResult { - try await Auth.auth().signIn(with: credential) + try await auth.signIn(with: credential) } private func updateUserProfile(displayName: String?, firstName: String?, lastName: String?, photoUrl: URL?) async throws -> User? { - let request = Auth.auth().currentUser?.createProfileChangeRequest() + let request = auth.currentUser?.createProfileChangeRequest() var didMakeChanges: Bool = false if let displayName { @@ -190,7 +201,7 @@ struct FirebaseAuthProvider: AuthProvider { try await request?.commitChanges() } - return Auth.auth().currentUser + return auth.currentUser } From b7826cbe5d0a713d3695cdc7bd88a365303e545b Mon Sep 17 00:00:00 2001 From: Justin Leger Date: Sun, 5 May 2024 11:44:51 -0500 Subject: [PATCH 2/2] Added support to initialize with a specific FirebaseApp instance. --- .../Core/AuthManager.swift | 11 +++++-- .../Providers/FirebaseAuthProvider.swift | 29 +++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift b/Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift index ee43177..838e75e 100644 --- a/Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift +++ b/Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift @@ -6,6 +6,7 @@ // import Foundation +import FirebaseAuth public struct AuthInfo { public let profile: UserAuthInfo? @@ -24,14 +25,18 @@ public enum MockAuthConfiguration { } public enum Configuration { - case mock(_ configuration: MockAuthConfiguration), firebase + case mock(_ configuration: MockAuthConfiguration) + case firebase + case firebaseAuth(auth: Auth) var provider: AuthProvider { switch self { - case .firebase: - return FirebaseAuthProvider() case .mock(let configuration): return MockAuthProvider(configuration: configuration) + case .firebase: + return FirebaseAuthProvider() + case .firebaseAuth(let auth): + return FirebaseAuthProvider(firebaseAuth: auth) } } } diff --git a/Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift b/Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift index 7cbb6ec..b86f443 100644 --- a/Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift +++ b/Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift @@ -6,12 +6,23 @@ // import Foundation +import Firebase import FirebaseAuth struct FirebaseAuthProvider: AuthProvider { + private var auth: Auth + + init() { + self.auth = Auth.auth() + } + + init(firebaseAuth: Auth) { + self.auth = firebaseAuth + } + func getAuthenticatedUser() -> UserAuthInfo? { - if let currentUser = Auth.auth().currentUser { + if let currentUser = auth.currentUser { return UserAuthInfo(user: currentUser) } else { return nil @@ -21,7 +32,7 @@ struct FirebaseAuthProvider: AuthProvider { @MainActor func authenticationDidChangeStream() -> AsyncStream { AsyncStream { continuation in - Auth.auth().addStateDidChangeListener { _, currentUser in + auth.addStateDidChangeListener { _, currentUser in if let currentUser { let user = UserAuthInfo(user: currentUser) continuation.yield(user) @@ -36,7 +47,7 @@ struct FirebaseAuthProvider: AuthProvider { func authenticateUser_Anonymously() async throws -> (user: UserAuthInfo, isNewUser: Bool) { // Sign in to Firebase - let authDataResult = try await Auth.auth().signInAnonymously() + let authDataResult = try await auth.signInAnonymously() // Determines if this is the first time this user is being authenticated let isNewUser = authDataResult.additionalUserInfo?.isNewUser ?? true @@ -161,11 +172,11 @@ struct FirebaseAuthProvider: AuthProvider { } func signOut() throws { - try Auth.auth().signOut() + try auth.signOut() } func deleteAccount() async throws { - guard let user = Auth.auth().currentUser else { + guard let user = auth.currentUser else { throw AuthError.userNotFound } @@ -177,15 +188,15 @@ struct FirebaseAuthProvider: AuthProvider { private func signInOrLink(credential: AuthCredential) async throws -> AuthDataResult { // If user is anonymous, attempt to link credential to existing account. On failure, fall-back to signIn to create a new account. - if let user = Auth.auth().currentUser, user.isAnonymous, let result = try? await user.link(with: credential) { + if let user = auth.currentUser, user.isAnonymous, let result = try? await user.link(with: credential) { return result } - return try await Auth.auth().signIn(with: credential) + return try await auth.signIn(with: credential) } private func updateUserProfile(displayName: String?, firstName: String?, lastName: String?, photoUrl: URL?) async throws -> User? { - let request = Auth.auth().currentUser?.createProfileChangeRequest() + let request = auth.currentUser?.createProfileChangeRequest() var didMakeChanges: Bool = false if let displayName { @@ -210,7 +221,7 @@ struct FirebaseAuthProvider: AuthProvider { try await request?.commitChanges() } - return Auth.auth().currentUser + return auth.currentUser }