diff --git a/SOPT-iOS/Projects/Data/Sources/Repository/CoreAuthRepository.swift b/SOPT-iOS/Projects/Data/Sources/Repository/CoreAuthRepository.swift index 40f8562f..41d26072 100644 --- a/SOPT-iOS/Projects/Data/Sources/Repository/CoreAuthRepository.swift +++ b/SOPT-iOS/Projects/Data/Sources/Repository/CoreAuthRepository.swift @@ -14,6 +14,7 @@ import Domain import Networks public struct CoreAuthRepository { + private let coreAuthService: CoreAuthService public init(coreAuthService: CoreAuthService) { @@ -28,9 +29,9 @@ extension CoreAuthRepository: CoreAuthRepositoryInterface { with identityToken: String ) -> AnyPublisher { coreAuthService - .login(.dto(token: identityToken, oauthType: provider)) + .login(.init(token: identityToken, authPlatform: provider.toData())) .compactMap { $0.data?.toDomain() } - .mapError { _ in CoreAuthError.makers(.loginFail) } + .mapError { _ in CoreAuthError.loginFail } .eraseToAnyPublisher() } diff --git a/SOPT-iOS/Projects/Data/Sources/Repository/CoreOAuthRepository.swift b/SOPT-iOS/Projects/Data/Sources/Repository/CoreOAuthRepository.swift index d50a49db..19a61ce5 100644 --- a/SOPT-iOS/Projects/Data/Sources/Repository/CoreOAuthRepository.swift +++ b/SOPT-iOS/Projects/Data/Sources/Repository/CoreOAuthRepository.swift @@ -24,10 +24,14 @@ public class CoreOAuthRepository { extension CoreOAuthRepository: CoreOAuthRepositoryInterface { public func getIdentityToken(from provider: OAuthType) -> AnyPublisher { + let service: AuthenticationService switch provider { - case .apple: return appleService.getIdentityToken() - case .google: fatalError() //TODO: + case .apple: service = appleService + case .google: fatalError() //TODO: } + return service.getIdentityToken() + .mapError { _ in CoreAuthError.oAuthFail(provider) } // oauth error의 구체화 필요시 여기서 구현 + .eraseToAnyPublisher() } } diff --git a/SOPT-iOS/Projects/Data/Sources/Transform/CoreLoginTransform.swift b/SOPT-iOS/Projects/Data/Sources/Transform/CoreLoginTransform.swift new file mode 100644 index 00000000..ce9c2d49 --- /dev/null +++ b/SOPT-iOS/Projects/Data/Sources/Transform/CoreLoginTransform.swift @@ -0,0 +1,18 @@ +// +// CoreLoginTransform.swift +// Data +// +// Created by 장석우 on 1/18/25. +// Copyright © 2025 SOPT-iOS. All rights reserved. +// + +import Foundation + +import Domain +import Networks + +extension CoreLoginEntity { + public func toDomain() -> CoreAuthTokens { + .init(accessToken: accessToken, refreshToken: refreshToken) + } +} diff --git a/SOPT-iOS/Projects/Data/Sources/Transform/LoginTransform.swift b/SOPT-iOS/Projects/Data/Sources/Transform/LoginTransform.swift deleted file mode 100644 index 465d3a97..00000000 --- a/SOPT-iOS/Projects/Data/Sources/Transform/LoginTransform.swift +++ /dev/null @@ -1,31 +0,0 @@ -// -// LoginTransform.swift -// Data -// -// Created by 장석우 on 1/18/25. -// Copyright © 2025 SOPT-iOS. All rights reserved. -// - -import Foundation - -import Domain -import Networks - -extension CoreLoginRequestEntity { - static func dto(token: String, oauthType: OAuthType) -> Self { - var platform: PlatformType - - switch oauthType { - case .apple: platform = .apple - case .google: platform = .google - } - - return .init(token: token, authPlatform: platform) - } -} - -extension CoreLoginEntity { - public func toDomain() -> CoreAuthTokens { - .init(accessToken: accessToken, refreshToken: refreshToken) - } -} diff --git a/SOPT-iOS/Projects/Data/Sources/Transform/OAuthTypeTransform.swift b/SOPT-iOS/Projects/Data/Sources/Transform/OAuthTypeTransform.swift new file mode 100644 index 00000000..73667e7a --- /dev/null +++ b/SOPT-iOS/Projects/Data/Sources/Transform/OAuthTypeTransform.swift @@ -0,0 +1,19 @@ +// +// OAuthTypeTransform.swift +// Data +// +// Created by 장석우 on 1/19/25. +// Copyright © 2025 SOPT-iOS. All rights reserved. +// + +import Domain +import Networks + +extension OAuthType { + public func toData() -> PlatformType { + switch self { + case .google: .google + case .apple: .apple + } + } +} diff --git a/SOPT-iOS/Projects/Domain/Sources/Error/CoreAuthError.swift b/SOPT-iOS/Projects/Domain/Sources/Error/CoreAuthError.swift index 45caac5e..e4157686 100644 --- a/SOPT-iOS/Projects/Domain/Sources/Error/CoreAuthError.swift +++ b/SOPT-iOS/Projects/Domain/Sources/Error/CoreAuthError.swift @@ -9,22 +9,7 @@ import Foundation public enum CoreAuthError: Error { - case apple(Apple) - case google(Google) - case makers(Makers) + case oAuthFail(OAuthType) + case loginFail case unknown(Error) - - public enum Apple: Error { - case authFail(Error) - case credentialFail - case encodedFail - } - - public enum Google: Error { - - } - - public enum Makers: Error { - case loginFail - } } diff --git a/SOPT-iOS/Projects/Domain/Sources/RepositoryInterface/CoreAuthRepositoryInterface.swift b/SOPT-iOS/Projects/Domain/Sources/RepositoryInterface/CoreAuthRepositoryInterface.swift index 9772b682..489da1af 100644 --- a/SOPT-iOS/Projects/Domain/Sources/RepositoryInterface/CoreAuthRepositoryInterface.swift +++ b/SOPT-iOS/Projects/Domain/Sources/RepositoryInterface/CoreAuthRepositoryInterface.swift @@ -9,7 +9,7 @@ import Combine public protocol CoreAuthRepositoryInterface { - func login(for provider: OAuthType,with identityToken: String) -> AnyPublisher + func login(for provider: OAuthType, with identityToken: String) -> AnyPublisher func changeSocialAccount() -> AnyPublisher func searchSocialAccount() -> AnyPublisher func signUp(_ model: SignUpModel) -> AnyPublisher diff --git a/SOPT-iOS/Projects/Domain/Sources/UseCase/SignInUseCase.swift b/SOPT-iOS/Projects/Domain/Sources/UseCase/SignInUseCase.swift index 4c3e21d4..875d1dfb 100644 --- a/SOPT-iOS/Projects/Domain/Sources/UseCase/SignInUseCase.swift +++ b/SOPT-iOS/Projects/Domain/Sources/UseCase/SignInUseCase.swift @@ -16,11 +16,13 @@ public enum SiginInHandleableType { } public protocol SignInUseCase { + // legacy func requestSignIn(token: String) - func login(with provider: OAuthType) -> AnyPublisher - - var sideEffect: PassthroughSubject { get } var signInSuccess: CurrentValueSubject { get set } + + // 인증 중앙화 + func login(with provider: OAuthType) -> AnyPublisher // 인증 중앙화 + var sideEffect: PassthroughSubject { get } // 인증 중앙화 } public class DefaultSignInUseCase { @@ -46,7 +48,9 @@ public class DefaultSignInUseCase { } //MARK: - 인증중앙화(CoreAuth) 로직 + extension DefaultSignInUseCase: SignInUseCase { + public func login(with provider: OAuthType) -> AnyPublisher { oauthRepository.getIdentityToken(from: .apple) .map { (provider, $0) } @@ -62,6 +66,7 @@ extension DefaultSignInUseCase: SignInUseCase { } //MARK: - LegacyAuth 로직 + extension DefaultSignInUseCase { public func requestSignIn(token: String) { repository.requestSignIn(token: token) diff --git a/SOPT-iOS/Projects/Features/AuthFeature/Sources/SignInScene/ViewModel/SignInViewModel.swift b/SOPT-iOS/Projects/Features/AuthFeature/Sources/SignInScene/ViewModel/SignInViewModel.swift index b217312e..d0a7e635 100644 --- a/SOPT-iOS/Projects/Features/AuthFeature/Sources/SignInScene/ViewModel/SignInViewModel.swift +++ b/SOPT-iOS/Projects/Features/AuthFeature/Sources/SignInScene/ViewModel/SignInViewModel.swift @@ -66,7 +66,12 @@ extension SignInViewModel { ).flatMap(useCase.login) .withUnretained(self) .sink { owner, _ in - print("로그인 성공") + print("로그인 성공했습니다.") + // 홈화면 진입 시 두가지 토큰이 충돌함 (2025.01.18) + // AS-IS: UserDefaultKeyList.Auth.appAccessToken + // TO-BE: UserDefaultKeyList.CoreAuth.accessToken + // 홈화면 진입 후 토큰 관리 로직을 AS-IS에서 TO-BE로 모두 변경 후 아래 코드 주석을 풀 것 +// owner.onSignInSuccess?(.loginSuccess) }.store(in: self.cancelBag) input.signUpButtonTapped diff --git a/SOPT-iOS/Projects/Modules/Networks/Sources/Entity/CoreAuth/VerifyType.swift b/SOPT-iOS/Projects/Modules/Networks/Sources/Entity/CoreAuth/VerifyType.swift index 32149cb4..7f3f0929 100644 --- a/SOPT-iOS/Projects/Modules/Networks/Sources/Entity/CoreAuth/VerifyType.swift +++ b/SOPT-iOS/Projects/Modules/Networks/Sources/Entity/CoreAuth/VerifyType.swift @@ -8,7 +8,6 @@ import Foundation - public enum VerifyType: String, Encodable { case register = "REGISTER" case change = "CHANGE" diff --git a/SOPT-iOS/Projects/Modules/Networks/Sources/Service/ASAuthorizationControllerProxy.swift b/SOPT-iOS/Projects/Modules/Networks/Sources/Service/ASAuthorizationControllerProxy.swift index 6aa638a6..c640f373 100644 --- a/SOPT-iOS/Projects/Modules/Networks/Sources/Service/ASAuthorizationControllerProxy.swift +++ b/SOPT-iOS/Projects/Modules/Networks/Sources/Service/ASAuthorizationControllerProxy.swift @@ -10,12 +10,10 @@ import AuthenticationServices import Combine import Foundation -import Domain - final class ASAuthorizationControllerProxy: NSObject { private let presentationWindow: UIWindow = UIWindow() - public let didComplete = PassthroughSubject() + public let didComplete = PassthroughSubject() private override init() {} @@ -24,10 +22,6 @@ final class ASAuthorizationControllerProxy: NSObject { object.delegate = owner return owner } - - deinit { - print("ASAuthorizationControllerProxy: 나 죽네") - } } extension ASAuthorizationControllerProxy: ASAuthorizationControllerDelegate { @@ -42,7 +36,7 @@ extension ASAuthorizationControllerProxy: ASAuthorizationControllerDelegate { controller: ASAuthorizationController, didCompleteWithError error: any Error ) { - didComplete.send(completion: .failure(.authFail(error))) + didComplete.send(completion: .failure(.unauthorized(error))) } } diff --git a/SOPT-iOS/Projects/Modules/Networks/Sources/Service/AppleAuthenticationService.swift b/SOPT-iOS/Projects/Modules/Networks/Sources/Service/AppleAuthenticationService.swift index 1dde21aa..ec7a10f1 100644 --- a/SOPT-iOS/Projects/Modules/Networks/Sources/Service/AppleAuthenticationService.swift +++ b/SOPT-iOS/Projects/Modules/Networks/Sources/Service/AppleAuthenticationService.swift @@ -10,10 +10,14 @@ import AuthenticationServices import Combine import Foundation -import Domain +public enum AuthenticationError: Error { + case unauthorized(Error) + case encodedFail + case unknown(Error) +} public protocol AuthenticationService { - func getIdentityToken() -> AnyPublisher + func getIdentityToken() -> AnyPublisher } public final class DefaultAppleAuthenticationService: AuthenticationService { @@ -28,33 +32,28 @@ public final class DefaultAppleAuthenticationService: AuthenticationService { private lazy var proxy = ASAuthorizationControllerProxy.proxy(for: authorizationController) - public func getIdentityToken() -> AnyPublisher { + public func getIdentityToken() -> AnyPublisher { performRequests() .tryMap { guard let credential = $0.credential as? ASAuthorizationAppleIDCredential, - let idToken = credential.identityToken - else { throw CoreAuthError.apple(.credentialFail) } - - guard let idTokenString = String(data: idToken, encoding: .utf8) - else { throw CoreAuthError.apple(.encodedFail) } + let idToken = credential.identityToken, + let idTokenString = String(data: idToken, encoding: .utf8) + else { throw AuthenticationError.encodedFail } return idTokenString } .mapError { - if let err = $0 as? CoreAuthError { return err } - else { return CoreAuthError.unknown($0) } + if let err = $0 as? AuthenticationError { return err } + else { return AuthenticationError.unknown($0) } } .eraseToAnyPublisher() } } extension DefaultAppleAuthenticationService { - private func performRequests() -> AnyPublisher { + private func performRequests() -> AnyPublisher { authorizationController.presentationContextProvider = proxy authorizationController.performRequests() - return proxy.didComplete - .mapError { - CoreAuthError.apple($0) - }.eraseToAnyPublisher() + return proxy.didComplete.eraseToAnyPublisher() } }