Skip to content

Commit 955a1aa

Browse files
authored
Merge branch 'develop' into feat/#35-swipe-todoviewmodel
2 parents c4e9f81 + 009a826 commit 955a1aa

10 files changed

Lines changed: 444 additions & 158 deletions

DevLog/Infra/Service/AuthService.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import FirebaseFirestore
1010

1111
final class AuthService {
1212
private let store = Firestore.firestore()
13+
private let logger = Logger(category: "AuthService")
1314

1415
var uid: String? {
1516
Auth.auth().currentUser?.uid
@@ -20,13 +21,25 @@ final class AuthService {
2021
}
2122

2223
func getProviderID() async throws -> String? {
23-
guard let uid = uid else { return nil }
24+
logger.info("Fetching current provider ID")
25+
26+
guard let uid = uid else {
27+
logger.warning("No user ID available")
28+
return nil
29+
}
2430

25-
let document = try await store
26-
.collection("users/\(uid)/userData")
27-
.document("info")
28-
.getDocument()
31+
do {
32+
let document = try await store
33+
.collection("users/\(uid)/userData")
34+
.document("info")
35+
.getDocument()
2936

30-
return document.data()?["currentProvider"] as? String
37+
let providerID = document.data()?["currentProvider"] as? String
38+
logger.info("Successfully fetched provider ID: \(providerID ?? "nil")")
39+
return providerID
40+
} catch {
41+
logger.error("Failed to fetch provider ID", error: error)
42+
throw error
43+
}
3144
}
3245
}

DevLog/Infra/Service/PushNotificationService.swift

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@ import FirebaseFirestore
1010

1111
final class PushNotificationService {
1212
private let store = Firestore.firestore()
13+
private let logger = Logger(category: "PushNotificationService")
1314

1415
/// 푸시 알림 On/Off 설정
1516
func fetchPushNotificationEnabled() async throws -> Bool {
17+
logger.info("Fetching push notification enabled status")
18+
1619
guard let uid = Auth.auth().currentUser?.uid else {
20+
logger.error("User not authenticated")
1721
throw AuthError.notAuthenticated
1822
}
1923

20-
let settingsRef = store.document("users/\(uid)/userData/settings")
21-
let doc = try await settingsRef.getDocument()
24+
do {
25+
let settingsRef = store.document("users/\(uid)/userData/settings")
26+
let doc = try await settingsRef.getDocument()
2227

23-
if let allowPush = doc.data()?["allowPushNotification"] as? Bool { return allowPush }
28+
if let allowPush = doc.data()?["allowPushNotification"] as? Bool {
29+
logger.info("Push notification enabled: \(allowPush)")
30+
return allowPush
31+
}
2432

25-
throw FirestoreError.dataNotFound("allowPushNotification")
33+
logger.error("Push notification setting not found")
34+
throw FirestoreError.dataNotFound("allowPushNotification")
35+
} catch {
36+
logger.error("Failed to fetch push notification status", error: error)
37+
throw error
38+
}
2639
}
2740

2841
/// 푸시 알림 시간 설정
@@ -47,23 +60,32 @@ final class PushNotificationService {
4760

4861
/// 푸시 알림 설정 업데이트
4962
func updatePushNotificationSettings(isEnabled: Bool, components: DateComponents) async throws {
63+
logger.info("Updating push notification settings - enabled: \(isEnabled)")
64+
5065
guard let uid = Auth.auth().currentUser?.uid else {
66+
logger.error("User not authenticated")
5167
throw AuthError.notAuthenticated
5268
}
5369

54-
let settingsRef = store.document("users/\(uid)/userData/settings")
70+
do {
71+
let settingsRef = store.document("users/\(uid)/userData/settings")
5572

56-
var dict: [String: Any] = ["allowPushNotification": isEnabled]
73+
var dict: [String: Any] = ["allowPushNotification": isEnabled]
5774

58-
if let hour = components.hour {
59-
dict["pushNotificationHour"] = hour
60-
}
75+
if let hour = components.hour {
76+
dict["pushNotificationHour"] = hour
77+
}
6178

62-
if let minute = components.minute {
63-
dict["pushNotificationMinute"] = minute
64-
}
79+
if let minute = components.minute {
80+
dict["pushNotificationMinute"] = minute
81+
}
6582

66-
try await settingsRef.setData(dict, merge: true)
83+
try await settingsRef.setData(dict, merge: true)
84+
logger.info("Successfully updated push notification settings")
85+
} catch {
86+
logger.error("Failed to update push notification settings", error: error)
87+
throw error
88+
}
6789
}
6890

6991
/// 푸시 알림 기록 요청

DevLog/Infra/Service/SocialLogin/AppleAuthenticationService.swift

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,29 @@ final class AppleAuthenticationService: AuthenticationService {
2020
private let messaging = Messaging.messaging()
2121
private var user: User? { Auth.auth().currentUser }
2222
private let providerID = AuthProviderID.apple
23+
private let logger = Logger(category: "AppleAuthService")
2324

2425
func signIn() async throws -> AuthenticationDataResponse {
25-
let response = try await authenticateWithAppleAsync()
26+
logger.info("Starting Apple sign in")
2627

27-
let nonce = response.nonce
28-
let credential = response.credential
29-
let authorizationCode = response.authorizationCode
30-
let idTokenString = response.idTokenString
31-
32-
// Firebase Function을 통해 customToken 요청
33-
let customToken = try await requestAppleCustomToken(
34-
idToken: idTokenString,
35-
authorizationCode: authorizationCode
36-
)
37-
38-
// customToken으로 Firebase 로그인
39-
let result = try await Auth.auth().signIn(withCustomToken: customToken)
28+
do {
29+
let response = try await authenticateWithAppleAsync()
30+
31+
let nonce = response.nonce
32+
let credential = response.credential
33+
let authorizationCode = response.authorizationCode
34+
let idTokenString = response.idTokenString
35+
36+
// Firebase Function을 통해 customToken 요청
37+
logger.debug("Requesting custom token from Firebase Function")
38+
let customToken = try await requestAppleCustomToken(
39+
idToken: idTokenString,
40+
authorizationCode: authorizationCode
41+
)
42+
43+
// customToken으로 Firebase 로그인
44+
logger.debug("Signing in with custom token")
45+
let result = try await Auth.auth().signIn(withCustomToken: customToken)
4046

4147
let changeRequest = result.user.createProfileChangeRequest()
4248
var displayName: String?
@@ -72,9 +78,14 @@ final class AppleAuthenticationService: AuthenticationService {
7278
try await result.user.link(with: appleCredential)
7379
}
7480

75-
let fcmToken = try await messaging.token()
81+
let fcmToken = try await messaging.token()
7682

77-
return result.user.toData(providerID: .apple, fcmToken: fcmToken)
83+
logger.info("Successfully signed in with Apple")
84+
return result.user.toData(providerID: .apple, fcmToken: fcmToken)
85+
} catch {
86+
logger.error("Failed to sign in with Apple", error: error)
87+
throw error
88+
}
7889
}
7990

8091
func signOut(_ uid: String) async throws {

DevLog/Infra/Service/SocialLogin/GithubAuthenticationService.swift

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@ final class GithubAuthenticationService: NSObject, AuthenticationService {
1818
private let messaging = Messaging.messaging()
1919
private var user: User? { Auth.auth().currentUser }
2020
private let providerID = AuthProviderID.gitHub
21+
private let logger = Logger(category: "GithubAuthService")
2122

2223
func signIn() async throws -> AuthenticationDataResponse {
23-
// 1. GitHub OAuth 로그인 요청
24-
let authorizationCode = try await requestAuthorizationCode()
25-
26-
// 2. Firebase Functions를 통해 customToken 발급 요청
27-
let (accessToken, customToken) = try await requestTokens(authorizationCode: authorizationCode)
24+
logger.info("Starting GitHub sign in")
2825

29-
// 3. Firebase 로그인
30-
let result = try await Auth.auth().signIn(withCustomToken: customToken)
26+
do {
27+
// 1. GitHub OAuth 로그인 요청
28+
logger.debug("Requesting authorization code")
29+
let authorizationCode = try await requestAuthorizationCode()
30+
31+
// 2. Firebase Functions를 통해 customToken 발급 요청
32+
logger.debug("Requesting tokens from Firebase Function")
33+
let (accessToken, customToken) = try await requestTokens(authorizationCode: authorizationCode)
34+
35+
// 3. Firebase 로그인
36+
logger.debug("Signing in with custom token")
37+
let result = try await Auth.auth().signIn(withCustomToken: customToken)
3138

3239
// 4. Firebase Auth 사용자 프로필 업데이트
3340
let githubUser = try await requestUserProfile(accessToken: accessToken)
@@ -45,13 +52,18 @@ final class GithubAuthenticationService: NSObject, AuthenticationService {
4552
try await result.user.link(with: credential)
4653
}
4754

48-
let fcmToken = try await messaging.token()
49-
50-
return result.user.toData(
51-
providerID: .gitHub,
52-
fcmToken: fcmToken,
53-
accessToken: accessToken
54-
)
55+
let fcmToken = try await messaging.token()
56+
57+
logger.info("Successfully signed in with GitHub")
58+
return result.user.toData(
59+
providerID: .gitHub,
60+
fcmToken: fcmToken,
61+
accessToken: accessToken
62+
)
63+
} catch {
64+
logger.error("Failed to sign in with GitHub", error: error)
65+
throw error
66+
}
5567
}
5668

5769
func signOut(_ uid: String) async throws {
@@ -79,36 +91,56 @@ final class GithubAuthenticationService: NSObject, AuthenticationService {
7991
}
8092

8193
func link(uid: String, email: String) async throws {
82-
let tokensRef = store.document("users/\(uid)/userData/tokens")
83-
let authorizationCode = try await requestAuthorizationCode()
84-
let (accessToken, _) = try await requestTokens(authorizationCode: authorizationCode)
94+
logger.info("Linking GitHub account for user: \(uid)")
95+
96+
do {
97+
let tokensRef = store.document("users/\(uid)/userData/tokens")
98+
let authorizationCode = try await requestAuthorizationCode()
99+
let (accessToken, _) = try await requestTokens(authorizationCode: authorizationCode)
85100

86-
let githubUser = try await requestUserProfile(accessToken: accessToken)
101+
let githubUser = try await requestUserProfile(accessToken: accessToken)
87102

88-
guard let githubEmail = githubUser.email else {
89-
try await revokeAccessToken(accessToken: accessToken)
90-
throw EmailFetchError.emailNotFound
91-
}
103+
guard let githubEmail = githubUser.email else {
104+
logger.error("GitHub email not found")
105+
try await revokeAccessToken(accessToken: accessToken)
106+
throw EmailFetchError.emailNotFound
107+
}
92108

93-
if githubEmail != email {
94-
try await revokeAccessToken(accessToken: accessToken)
95-
throw EmailFetchError.emailMismatch
96-
}
109+
if githubEmail != email {
110+
logger.error("Email mismatch - Expected: \(email), Got: \(githubEmail)")
111+
try await revokeAccessToken(accessToken: accessToken)
112+
throw EmailFetchError.emailMismatch
113+
}
97114

98-
try await tokensRef.setData(["githubAccessToken": accessToken], merge: true)
115+
try await tokensRef.setData(["githubAccessToken": accessToken], merge: true)
99116

100-
let credential = OAuthProvider.credential(providerID: AuthProviderID.gitHub, accessToken: accessToken)
101-
try await user?.link(with: credential)
117+
let credential = OAuthProvider.credential(providerID: AuthProviderID.gitHub, accessToken: accessToken)
118+
try await user?.link(with: credential)
119+
120+
logger.info("Successfully linked GitHub account")
121+
} catch {
122+
logger.error("Failed to link GitHub account", error: error)
123+
throw error
124+
}
102125
}
103126

104127
func unlink(_ uid: String) async throws {
105-
try await revokeAccessToken()
128+
logger.info("Unlinking GitHub account for user: \(uid)")
129+
130+
do {
131+
try await revokeAccessToken()
106132

107-
let tokensRef = store.document("users/\(uid)/userData/tokens")
133+
let tokensRef = store.document("users/\(uid)/userData/tokens")
108134

109-
try await tokensRef.updateData(["githubAccessToken": FieldValue.delete()])
135+
try await tokensRef.updateData(["githubAccessToken": FieldValue.delete()])
110136

111-
_ = try await user?.unlink(fromProvider: providerID.rawValue)
137+
_ = try await user?.unlink(fromProvider: providerID.rawValue)
138+
139+
logger.info("Successfully unlinked GitHub account")
140+
} catch {
141+
logger.error("Failed to unlink GitHub account", error: error)
142+
throw error
143+
}
112144
}
113145

114146
func requestAuthorizationCode() async throws -> String {

DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,47 @@ final class GoogleAuthenticationService: AuthenticationService {
1818
private let messaging = Messaging.messaging()
1919
private var user: User? { Auth.auth().currentUser }
2020
private let provider = TopViewControllerProvider()
21+
private let logger = Logger(category: "GoogleAuthService")
2122

2223
@MainActor
2324
func signIn() async throws -> AuthenticationDataResponse {
25+
logger.info("Starting Google sign in")
26+
2427
guard let topViewController = provider.topViewController() else {
28+
logger.error("Top view controller not found")
2529
throw UIError.notFoundTopViewController
2630
}
2731

28-
let signIn = try await GIDSignIn.sharedInstance.signIn(withPresenting: topViewController)
29-
30-
guard let idToken = signIn.user.idToken?.tokenString else {
31-
throw URLError(.badServerResponse)
32-
}
33-
34-
let accessToken = signIn.user.accessToken.tokenString
35-
let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
36-
37-
let result = try await Auth.auth().signIn(with: credential)
38-
39-
if let photoURL = signIn.user.profile?.imageURL(withDimension: 200) {
40-
let changeRequest = result.user.createProfileChangeRequest()
41-
changeRequest.photoURL = photoURL
42-
changeRequest.displayName = signIn.user.profile?.name
43-
44-
try await changeRequest.commitChanges()
32+
do {
33+
let signIn = try await GIDSignIn.sharedInstance.signIn(withPresenting: topViewController)
34+
35+
guard let idToken = signIn.user.idToken?.tokenString else {
36+
logger.error("ID token not found")
37+
throw URLError(.badServerResponse)
38+
}
39+
40+
let accessToken = signIn.user.accessToken.tokenString
41+
let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
42+
43+
logger.debug("Signing in with Google credential")
44+
let result = try await Auth.auth().signIn(with: credential)
45+
46+
if let photoURL = signIn.user.profile?.imageURL(withDimension: 200) {
47+
let changeRequest = result.user.createProfileChangeRequest()
48+
changeRequest.photoURL = photoURL
49+
changeRequest.displayName = signIn.user.profile?.name
50+
51+
try await changeRequest.commitChanges()
52+
}
53+
54+
let fcmToken = try await messaging.token()
55+
56+
logger.info("Successfully signed in with Google")
57+
return result.user.toData(providerID: .google, fcmToken: fcmToken)
58+
} catch {
59+
logger.error("Failed to sign in with Google", error: error)
60+
throw error
4561
}
46-
47-
let fcmToken = try await messaging.token()
48-
49-
return result.user.toData(providerID: .google, fcmToken: fcmToken)
5062
}
5163

5264
func signOut(_ uid: String) async throws {

0 commit comments

Comments
 (0)