Skip to content

Commit

Permalink
[Feat/#91] LoginUseCase 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-seonwoo committed Nov 8, 2024
1 parent cb8bfb4 commit 0c4c25b
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// LoginUseCase.swift
// LoginFeature
//
// Created by Seonwoo Kim on 11/8/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Foundation
import Combine

import Domain
import Core

public enum LoginResponseType {
case loginSuccess
case loginFailure
case onboardingNeeded
}

public protocol LoginUseCaseType {
func login(provider: OAuthProviderType) -> AnyPublisher<LoginResponseType, AuthError>
}

public final class LoginUseCase: LoginUseCaseType {

private let repository: AuthRepositoryType

public init(repository: AuthRepositoryType) {
self.repository = repository
}

public func login(provider: Domain.OAuthProviderType) -> AnyPublisher<LoginResponseType, Domain.AuthError> {
repository.authorize(provider)
.handleEvents(receiveOutput: { socialToken in
UserManager.shared.socialToken = socialToken
})
.flatMap { [weak self] _ -> AnyPublisher<LoginResponseType, Domain.AuthError> in
guard let self = self else {
return Fail(error: Domain.AuthError.appleAuthrizeError).eraseToAnyPublisher()
}

return self.repository.socialLogin(socialPlatform: provider.rawValue)
.map { _ in LoginResponseType.loginSuccess }
.catch { error -> AnyPublisher<LoginResponseType, Domain.AuthError> in
switch error {
case .alreadyRegisteredUser:
return Just(.onboardingNeeded)
.setFailureType(to: Domain.AuthError.self)
.eraseToAnyPublisher()
default:
return Just(.loginFailure)
.setFailureType(to: Domain.AuthError.self)
.eraseToAnyPublisher()
}
}
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
}

0 comments on commit 0c4c25b

Please sign in to comment.