-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthDataRepositoryImpl.swift
More file actions
99 lines (85 loc) · 2.82 KB
/
Copy pathAuthDataRepositoryImpl.swift
File metadata and controls
99 lines (85 loc) · 2.82 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
//
// AuthDataRepositoryImpl.swift
// DevLogData
//
// Created by 최윤진 on 2/12/26.
//
import DevLogDomain
final class AuthDataRepositoryImpl: AuthDataRepository {
private let authService: AuthService
private let appleAuthService: AuthenticationService
private let githubAuthService: AuthenticationService
private let googleAuthService: AuthenticationService
init(
authService: AuthService,
appleAuthService: AuthenticationService,
githubAuthService: AuthenticationService,
googleAuthService: AuthenticationService
) {
self.authService = authService
self.appleAuthService = appleAuthService
self.githubAuthService = githubAuthService
self.googleAuthService = googleAuthService
}
func fetchCurrentProvider() async throws -> AuthProvider? {
guard let providerString = try await authService.getProviderID() else {
return nil
}
return AuthProvider(rawValue: providerString)
}
func fetchAllProviders() async throws -> [AuthProvider] {
let providerStrings = authService.providerIDs
return providerStrings.compactMap { AuthProvider(rawValue: $0) }
}
func linkProvider(_ provider: AuthProvider) async throws {
guard let uid = authService.uid,
let email = authService.currentUserEmail else {
throw AuthError.notAuthenticated
}
let service: AuthenticationService
switch provider {
case .apple:
service = appleAuthService
case .google:
service = googleAuthService
case .github:
service = githubAuthService
}
do {
try await service.link(uid: uid, email: email)
} catch {
throw mapLinkError(error)
}
}
func unlinkProvider(_ provider: AuthProvider) async throws {
guard let uid = authService.uid else {
throw AuthError.notAuthenticated
}
if authService.providerCount <= 1 {
throw AuthError.failedToUnlinkLastProvider
}
let service: AuthenticationService
switch provider {
case .apple:
service = appleAuthService
case .google:
service = googleAuthService
case .github:
service = githubAuthService
}
try await service.unlink(uid)
}
}
private extension AuthDataRepositoryImpl {
func mapLinkError(_ error: Error) -> Error {
if let emailFetchError = error as? EmailFetchError {
switch emailFetchError {
case .emailNotFound:
return AuthError.linkEmailNotFound
case .emailMismatch:
return AuthError.linkEmailMismatch
}
}
return error
}
}