Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Sources/SwiftfulFirebaseAuth/Core/AuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import FirebaseAuth

public struct AuthInfo {
public let profile: UserAuthInfo?
Expand All @@ -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)
}
}
}
Expand Down
29 changes: 20 additions & 9 deletions Sources/SwiftfulFirebaseAuth/Providers/FirebaseAuthProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Comment on lines +20 to +22

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't done this in a project myself. How do you get an instance of Auth other than calling Auth.auth() ? Do you have a sample implementation on how you'd initialize this?

@justbcuz justbcuz May 16, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can start off with configuring a FirebaseApp with custom options.
FirebaseApp.configure(name: appName, options: options)

Then you can access the FirebaseApp by name
let firebaseApp = FirebaseApp.app(name: appName)

Then you can instanciate an Auth instance with FirebaseApp
let firebaseAuth = Auth.auth(app: firebaseApp)

Same thing can be done with Firestore and Storage
let firebaseFirestore = Firestore.firestore(app: firebaseApp)
firebaseStorage = Storage.storage(app: firebaseApp)


func getAuthenticatedUser() -> UserAuthInfo? {
if let currentUser = Auth.auth().currentUser {
if let currentUser = auth.currentUser {
return UserAuthInfo(user: currentUser)
} else {
return nil
Expand All @@ -21,7 +32,7 @@ struct FirebaseAuthProvider: AuthProvider {
@MainActor
func authenticationDidChangeStream() -> AsyncStream<UserAuthInfo?> {
AsyncStream { continuation in
Auth.auth().addStateDidChangeListener { _, currentUser in
auth.addStateDidChangeListener { _, currentUser in
if let currentUser {
let user = UserAuthInfo(user: currentUser)
continuation.yield(user)
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -210,7 +221,7 @@ struct FirebaseAuthProvider: AuthProvider {
try await request?.commitChanges()
}

return Auth.auth().currentUser
return auth.currentUser
}


Expand Down