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 Oct 9, 2024
1 parent 041a82d commit 1f3b9f4
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// LoginUseCase.swift
// LoginFeatureInterface
//
// Created by Seonwoo Kim on 10/9/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Foundation
import Combine
import Core
import Domain

public enum LoginResponseType {
case loginSuccess
case loginFailure
case noUserInfo
}

protocol LoginUseCaseType {
func requestLogin(platform: String, socialToken: String) -> AnyPublisher<SocialLogineResponseDTO, Error>
var loginResponse: CurrentValueSubject<LoginResponseType, Error> { get set }
}

final class LoginUseCase: LoginUseCaseType {

public var loginResponse = CurrentValueSubject<LoginResponseType, Error>(.loginFailure)


private var container: DIContainer
private var cancelBag = CancelBag()

init(container: DIContainer) {
self.container = container
}

func requestLogin(platform: String, socialToken: String) -> AnyPublisher<Domain.SocialLogineResponseDTO, any Error> {
return container.services.userService.signIn(platform: platform)
.handleEvents(receiveOutput: { response in
UserManager.shared.socialToken = socialToken
UserManager.shared.accessToken = response.data
UserManager.shared.refreshToken = response.data
})
.map { response in
handleLoginResponse(statusCode: response.statusCode)
}
.eraseToAnyPublisher()
}

private func handleLoginResponse(statusCode: Int) -> LoginResponseType {
switch statusCode {
case 200..<300:
self.loginResponse.send(.loginSuccess)
return .loginSuccess
case 403:
self.loginResponse.send(.noUserInfo)
return .noUserInfo
default:
self.loginResponse.send(.loginFailure)
return .loginFailure
}
}
}

0 comments on commit 1f3b9f4

Please sign in to comment.