|
| 1 | +// |
| 2 | +// AuthDataRepositoryImpl.swift |
| 3 | +// DevLog |
| 4 | +// |
| 5 | +// Created by 최윤진 on 2/12/26. |
| 6 | +// |
| 7 | + |
| 8 | +import FirebaseAuth |
| 9 | + |
| 10 | +final class AuthDataRepositoryImpl: AuthDataRepository { |
| 11 | + private let authService: AuthService |
| 12 | + private let appleAuthService: AuthenticationService |
| 13 | + private let githubAuthService: AuthenticationService |
| 14 | + private let googleAuthService: AuthenticationService |
| 15 | + |
| 16 | + init( |
| 17 | + authService: AuthService, |
| 18 | + appleAuthService: AuthenticationService, |
| 19 | + githubAuthService: AuthenticationService, |
| 20 | + googleAuthService: AuthenticationService |
| 21 | + ) { |
| 22 | + self.authService = authService |
| 23 | + self.appleAuthService = appleAuthService |
| 24 | + self.githubAuthService = githubAuthService |
| 25 | + self.googleAuthService = googleAuthService |
| 26 | + } |
| 27 | + |
| 28 | + func fetchCurrentProvider() async throws -> AuthProvider? { |
| 29 | + guard let providerString = try await authService.getProviderID() else { |
| 30 | + return nil |
| 31 | + } |
| 32 | + return AuthProvider(rawValue: providerString) |
| 33 | + } |
| 34 | + |
| 35 | + func fetchAllProviders() async throws -> [AuthProvider] { |
| 36 | + let providerStrings = authService.providerIDs ?? [] |
| 37 | + return providerStrings.compactMap { AuthProvider(rawValue: $0) } |
| 38 | + } |
| 39 | + |
| 40 | + func linkProvider(_ provider: AuthProvider) async throws { |
| 41 | + guard let uid = authService.uid, |
| 42 | + let user = Auth.auth().currentUser, |
| 43 | + let email = user.email else { |
| 44 | + throw AuthError.notAuthenticated |
| 45 | + } |
| 46 | + |
| 47 | + let service: AuthenticationService |
| 48 | + switch provider { |
| 49 | + case .apple: |
| 50 | + service = appleAuthService |
| 51 | + case .google: |
| 52 | + service = googleAuthService |
| 53 | + case .github: |
| 54 | + service = githubAuthService |
| 55 | + } |
| 56 | + |
| 57 | + try await service.link(uid: uid, email: email) |
| 58 | + } |
| 59 | + |
| 60 | + func unlinkProvider(_ provider: AuthProvider) async throws { |
| 61 | + guard let uid = authService.uid, |
| 62 | + let user = Auth.auth().currentUser else { |
| 63 | + throw AuthError.notAuthenticated |
| 64 | + } |
| 65 | + |
| 66 | + if user.providerData.count <= 1 { |
| 67 | + throw AuthError.failedToUnlinkLastProvider |
| 68 | + } |
| 69 | + |
| 70 | + let service: AuthenticationService |
| 71 | + switch provider { |
| 72 | + case .apple: |
| 73 | + service = appleAuthService |
| 74 | + case .google: |
| 75 | + service = googleAuthService |
| 76 | + case .github: |
| 77 | + service = githubAuthService |
| 78 | + } |
| 79 | + |
| 80 | + try await service.unlink(uid) |
| 81 | + } |
| 82 | +} |
0 commit comments