-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationRepositoryImpl.swift
More file actions
132 lines (115 loc) · 4 KB
/
Copy pathAuthenticationRepositoryImpl.swift
File metadata and controls
132 lines (115 loc) · 4 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// AuthenticationRepositoryImpl.swift
// DevLogData
//
// Created by 최윤진 on 11/2/25.
//
import DevLogDomain
final class AuthenticationRepositoryImpl: AuthenticationRepository {
private let authService: AuthService
private let appleAuthService: AuthenticationService
private let githubAuthService: AuthenticationService
private let googleAuthService: AuthenticationService
private let userService: UserService
private let widgetSnapshotUpdater: WidgetSnapshotUpdater
init(
authService: AuthService,
appleAuthService: AuthenticationService,
githubAuthService: AuthenticationService,
googleAuthService: AuthenticationService,
userService: UserService,
widgetSnapshotUpdater: WidgetSnapshotUpdater
) {
self.authService = authService
self.appleAuthService = appleAuthService
self.githubAuthService = githubAuthService
self.googleAuthService = googleAuthService
self.userService = userService
self.widgetSnapshotUpdater = widgetSnapshotUpdater
}
func signIn(_ provider: AuthProvider) async throws {
authService.beginSignIn()
do {
let response: AuthDataResponse
switch provider {
case .apple:
response = try await appleAuthService.signIn()
case .github:
response = try await githubAuthService.signIn()
case .google:
response = try await googleAuthService.signIn()
}
try await userService.upsertUser(response)
authService.completeSignIn()
} catch {
if authService.uid != nil {
try? await authService.clearCurrentSession()
}
authService.cancelSignIn()
throw mapSignInError(error)
}
}
func signOut() async throws {
guard let uid = authService.uid,
let providerID = try await authService.getProviderID(),
let provider = AuthProvider(rawValue: providerID)
else {
try await authService.clearCurrentSession()
widgetSnapshotUpdater.clear()
return
}
do {
switch provider {
case .apple:
try await appleAuthService.signOut(uid)
case .github:
try await githubAuthService.signOut(uid)
case .google:
try await googleAuthService.signOut(uid)
}
} catch {
if case AuthError.notAuthenticated = error.toDomain() {
try await authService.clearCurrentSession()
} else {
throw error.toDomain()
}
}
widgetSnapshotUpdater.clear()
}
func restore() -> Bool {
// MARK: 후에 Google API를 사용 시 Google만의 restorePreviousSignIn 로직 추가
return authService.uid != nil
}
func delete() async throws {
guard let uid = authService.uid else {
throw AuthError.notAuthenticated
}
let providers = authService.providerIDs.compactMap { AuthProvider(rawValue: $0) }
for provider in providers {
switch provider {
case .apple:
try await appleAuthService.deleteAuth(uid)
case .github:
try await githubAuthService.deleteAuth(uid)
case .google:
try await googleAuthService.deleteAuth(uid)
}
}
do {
try await authService.deleteCurrentUser()
try await authService.clearCurrentSession()
widgetSnapshotUpdater.clear()
} catch {
throw error.toDomain()
}
}
}
private extension AuthenticationRepositoryImpl {
func mapSignInError(_ error: Error) -> Error {
if let emailFetchError = error as? EmailFetchError,
emailFetchError == .emailNotFound {
return AuthError.emailNotFound
}
return error.toDomain()
}
}