Skip to content

Commit

Permalink
[Feat] #468 - 클린아키텍처에 맞게 의존성 및 매핑 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
meltsplit committed Jan 18, 2025
1 parent b167eff commit 5358266
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Domain
import Networks

public struct CoreAuthRepository {

private let coreAuthService: CoreAuthService

public init(coreAuthService: CoreAuthService) {
Expand All @@ -28,9 +29,9 @@ extension CoreAuthRepository: CoreAuthRepositoryInterface {
with identityToken: String
) -> AnyPublisher<CoreAuthTokens, CoreAuthError> {
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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public class CoreOAuthRepository {

extension CoreOAuthRepository: CoreOAuthRepositoryInterface {
public func getIdentityToken(from provider: OAuthType) -> AnyPublisher<String, Domain.CoreAuthError> {
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()
}
}
18 changes: 18 additions & 0 deletions SOPT-iOS/Projects/Data/Sources/Transform/CoreLoginTransform.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
31 changes: 0 additions & 31 deletions SOPT-iOS/Projects/Data/Sources/Transform/LoginTransform.swift

This file was deleted.

19 changes: 19 additions & 0 deletions SOPT-iOS/Projects/Data/Sources/Transform/OAuthTypeTransform.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
19 changes: 2 additions & 17 deletions SOPT-iOS/Projects/Domain/Sources/Error/CoreAuthError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Combine

public protocol CoreAuthRepositoryInterface {
func login(for provider: OAuthType,with identityToken: String) -> AnyPublisher<CoreAuthTokens, CoreAuthError>
func login(for provider: OAuthType, with identityToken: String) -> AnyPublisher<CoreAuthTokens, CoreAuthError>
func changeSocialAccount() -> AnyPublisher<Void, CoreAuthError>
func searchSocialAccount() -> AnyPublisher<Void, CoreAuthError>
func signUp(_ model: SignUpModel) -> AnyPublisher<Void, CoreAuthError>
Expand Down
11 changes: 8 additions & 3 deletions SOPT-iOS/Projects/Domain/Sources/UseCase/SignInUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ public enum SiginInHandleableType {
}

public protocol SignInUseCase {
// legacy
func requestSignIn(token: String)
func login(with provider: OAuthType) -> AnyPublisher<Void, Never>

var sideEffect: PassthroughSubject<CoreAuthError, Never> { get }
var signInSuccess: CurrentValueSubject<SiginInHandleableType, Error> { get set }

// 인증 중앙화
func login(with provider: OAuthType) -> AnyPublisher<Void, Never> // 인증 중앙화
var sideEffect: PassthroughSubject<CoreAuthError, Never> { get } // 인증 중앙화
}

public class DefaultSignInUseCase {
Expand All @@ -46,7 +48,9 @@ public class DefaultSignInUseCase {
}

//MARK: - 인증중앙화(CoreAuth) 로직

extension DefaultSignInUseCase: SignInUseCase {

public func login(with provider: OAuthType) -> AnyPublisher<Void, Never> {
oauthRepository.getIdentityToken(from: .apple)
.map { (provider, $0) }
Expand All @@ -62,6 +66,7 @@ extension DefaultSignInUseCase: SignInUseCase {
}

//MARK: - LegacyAuth 로직

extension DefaultSignInUseCase {
public func requestSignIn(token: String) {
repository.requestSignIn(token: token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Foundation


public enum VerifyType: String, Encodable {
case register = "REGISTER"
case change = "CHANGE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ASAuthorization, CoreAuthError.Apple>()
public let didComplete = PassthroughSubject<ASAuthorization, AuthenticationError>()

private override init() {}

Expand All @@ -24,10 +22,6 @@ final class ASAuthorizationControllerProxy: NSObject {
object.delegate = owner
return owner
}

deinit {
print("ASAuthorizationControllerProxy: 나 죽네")
}
}

extension ASAuthorizationControllerProxy: ASAuthorizationControllerDelegate {
Expand All @@ -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)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, CoreAuthError>
func getIdentityToken() -> AnyPublisher<String, AuthenticationError>
}

public final class DefaultAppleAuthenticationService: AuthenticationService {
Expand All @@ -28,33 +32,28 @@ public final class DefaultAppleAuthenticationService: AuthenticationService {

private lazy var proxy = ASAuthorizationControllerProxy.proxy(for: authorizationController)

public func getIdentityToken() -> AnyPublisher<String, CoreAuthError> {
public func getIdentityToken() -> AnyPublisher<String, AuthenticationError> {
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<ASAuthorization, CoreAuthError> {
private func performRequests() -> AnyPublisher<ASAuthorization, AuthenticationError> {
authorizationController.presentationContextProvider = proxy
authorizationController.performRequests()
return proxy.didComplete
.mapError {
CoreAuthError.apple($0)
}.eraseToAnyPublisher()
return proxy.didComplete.eraseToAnyPublisher()
}
}

0 comments on commit 5358266

Please sign in to comment.