-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthDataRepositoryImpl.swift
More file actions
82 lines (70 loc) · 2.42 KB
/
AuthDataRepositoryImpl.swift
File metadata and controls
82 lines (70 loc) · 2.42 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
//
// AuthDataRepositoryImpl.swift
// DevLog
//
// Created by 최윤진 on 2/12/26.
//
import FirebaseAuth
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 user = Auth.auth().currentUser,
let email = user.email else {
throw AuthError.notAuthenticated
}
let service: AuthenticationService
switch provider {
case .apple:
service = appleAuthService
case .google:
service = googleAuthService
case .github:
service = githubAuthService
}
try await service.link(uid: uid, email: email)
}
func unlinkProvider(_ provider: AuthProvider) async throws {
guard let uid = authService.uid,
let user = Auth.auth().currentUser else {
throw AuthError.notAuthenticated
}
if user.providerData.count <= 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)
}
}