-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthService.swift
More file actions
74 lines (59 loc) · 2.07 KB
/
AuthService.swift
File metadata and controls
74 lines (59 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// AuthService.swift
// DevLog
//
// Created by 최윤진 on 11/29/25.
//
import FirebaseAuth
import FirebaseFirestore
import FirebaseFunctions
import FirebaseMessaging
final class AuthService {
private let store = Firestore.firestore()
private let functions = Functions.functions(region: "asia-northeast3")
private let messaging = Messaging.messaging()
private let logger = Logger(category: "AuthService")
var uid: String? {
Auth.auth().currentUser?.uid
}
var providerIDs: [String] {
Auth.auth().currentUser?.providerData.map { $0.providerID } ?? []
}
func getProviderID() async throws -> String? {
logger.info("Fetching current provider ID")
guard let uid = uid else {
logger.warning("No user ID available")
return nil
}
do {
let document = try await store
.collection("users/\(uid)/userData")
.document("info")
.getDocument()
let providerID = document.data()?["currentProvider"] as? String
logger.info("Successfully fetched provider ID: \(providerID ?? "nil")")
return providerID
} catch {
logger.error("Failed to fetch provider ID", error: error)
throw error
}
}
func deleteFirestoreUserData(_ uid: String) async throws {
logger.info("Deleting Firestore user data. uid: \(uid)")
let deleteFunction = functions.httpsCallable("deleteUserFirestoreData")
_ = try await deleteFunction.call(["uid": uid])
}
func deleteCurrentUser() async throws {
logger.info("Deleting FirebaseAuth current user")
guard let currentUser = Auth.auth().currentUser else {
logger.warning("No current user to delete")
throw AuthError.notAuthenticated
}
try await currentUser.delete()
}
func clearCurrentSession() async throws {
logger.info("Clearing current auth session")
try await messaging.deleteToken()
try Auth.auth().signOut()
}
}