-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb8bfb4
commit 0c4c25b
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
HMH_Tuist_iOS/Projects/Features/LoginFeature/Sources/LoginUseCase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
|