-
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.
Merge branch 'develop' into feat/#118
- Loading branch information
Showing
7 changed files
with
135 additions
and
136 deletions.
There are no files selected for viewing
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
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
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: OAuthProviderType) -> AnyPublisher<LoginResponseType, Domain.AuthError> { | ||
repository.authorize(provider) | ||
.handleEvents(receiveOutput: { socialToken in | ||
UserManager.shared.socialToken = socialToken | ||
}) | ||
.flatMap { [weak self] _ -> AnyPublisher<LoginResponseType, AuthError> in | ||
guard let self = self else { | ||
return Fail(error: AuthError.appleAuthrizeError).eraseToAnyPublisher() | ||
} | ||
|
||
return self.repository.socialLogin(socialPlatform: provider.rawValue) | ||
.map { _ in LoginResponseType.loginSuccess } | ||
.catch { error -> AnyPublisher<LoginResponseType, AuthError> in | ||
switch error { | ||
case .unregisteredUser: | ||
return Just(.onboardingNeeded) | ||
.setFailureType(to: AuthError.self) | ||
.eraseToAnyPublisher() | ||
default: | ||
return Just(.loginFailure) | ||
.setFailureType(to: AuthError.self) | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
|
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
131 changes: 45 additions & 86 deletions
131
HMH_Tuist_iOS/Projects/Features/LoginFeature/Sources/ViewModels/LoginViewModel.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 |
---|---|---|
@@ -1,106 +1,65 @@ | ||
import SwiftUI | ||
import AuthenticationServices | ||
// | ||
// LoginViewModel.swift | ||
// LoginFeature | ||
// | ||
// Created by Seonwoo Kim on 11/8/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import KakaoSDKUser | ||
import Foundation | ||
import Combine | ||
|
||
import Core | ||
import Domain | ||
import DSKit | ||
|
||
public class LoginViewModel: NSObject, ObservableObject { | ||
public final class LoginViewModel: ObservableObject { | ||
|
||
@Published public var isLoading: Bool = true | ||
@Published var isPresented: Bool = false | ||
@Published var alertType: CustomAlertType = .unlock | ||
private let loginUseCase: LoginUseCaseType | ||
private var cancelBag = CancelBag() | ||
|
||
public func handleSplashScreen() { | ||
self.isLoading = false | ||
} | ||
// 화면 이동 로직과 스와이프 인덱스 포함 | ||
@Published private(set) var state = State(loginStatus: .loginFailure, swipeImageIndex: 0) | ||
|
||
func handleAppleLogin() { | ||
let request = ASAuthorizationAppleIDProvider().createRequest() | ||
request.requestedScopes = [.fullName, .email] | ||
|
||
let authorizationController = ASAuthorizationController(authorizationRequests: [request]) | ||
authorizationController.delegate = self | ||
authorizationController.performRequests() | ||
public init(loginUseCase: LoginUseCaseType) { | ||
self.loginUseCase = loginUseCase | ||
startImageTimer() | ||
} | ||
|
||
func handleKakaoLogin() { | ||
if (UserApi.isKakaoTalkLoginAvailable()) { | ||
UserApi.shared.loginWithKakaoTalk {(oauthToken, error) in | ||
if let error = error { | ||
print(error) | ||
} | ||
if let oauthToken = oauthToken{ | ||
let idToken = oauthToken.accessToken | ||
UserManager.shared.socialPlatform = "KAKAO" | ||
UserManager.shared.socialToken = "Bearer " + idToken | ||
self.postSocialLoginData() | ||
} | ||
} | ||
} else { | ||
UserApi.shared.loginWithKakaoAccount {(oauthToken, error) in | ||
if let error = error { | ||
print("🍀",error) | ||
} | ||
if let oauthToken = oauthToken{ | ||
print("kakao success") | ||
UserManager.shared.socialPlatform = "KAKAO" | ||
let idToken = oauthToken.accessToken | ||
UserManager.shared.socialToken = "Bearer " + idToken | ||
self.postSocialLoginData() | ||
} | ||
} | ||
} | ||
// MARK: Action | ||
|
||
enum Action { | ||
case loginButtonDidTap(provider: OAuthProviderType) | ||
case swipeButtonDidTap(index: Int) | ||
} | ||
|
||
//TODO: 네트워크 부분은 의존성 정리한 뒤에 다시 연결해봅시다 | ||
func postSocialLoginData() { | ||
// let provider = Providers.AuthProvider | ||
// let request = SocialLoginRequestDTO(socialPlatform: UserManager.shared.socialPlatform ?? "") | ||
// | ||
// provider.request(target: .socialLogin(data: request), instance: BaseResponse<SocialLogineResponseDTO>.self) { data in | ||
// if data.status == 403 { | ||
// UserManager.shared.appStateString = "onboarding" | ||
// } else if data.status == 200 { | ||
// guard let data = data.data else { return } | ||
// UserManager.shared.refreshToken = data.token.refreshToken | ||
// UserManager.shared.accessToken = data.token.accessToken | ||
// UserManager.shared.appStateString = "home" | ||
// } | ||
// } | ||
// MARK: State | ||
|
||
struct State { | ||
var loginStatus: LoginResponseType | ||
var swipeImageIndex: Int | ||
} | ||
} | ||
|
||
extension LoginViewModel: ASAuthorizationControllerDelegate { | ||
|
||
public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { | ||
switch authorization.credential { | ||
case let appleIDCredential as ASAuthorizationAppleIDCredential: | ||
let userIdentifier = appleIDCredential.user | ||
let fullName = appleIDCredential.fullName | ||
|
||
if let identityToken = appleIDCredential.identityToken, | ||
let identifyTokenString = String(data: identityToken, encoding: .utf8) { | ||
UserManager.shared.socialToken = identifyTokenString | ||
UserManager.shared.socialPlatform = "APPLE" | ||
self.postSocialLoginData() | ||
} else { | ||
print("Identity token is nil or failed to convert to string.") | ||
} | ||
default: | ||
break | ||
func send(action: Action) { | ||
switch action { | ||
case .loginButtonDidTap(let provider): | ||
loginUseCase.login(provider: provider) | ||
.sink(receiveCompletion: { _ in }) { [weak self] response in | ||
self?.state.loginStatus = response | ||
} | ||
.store(in: cancelBag) | ||
case .swipeButtonDidTap(let index): | ||
self.state.swipeImageIndex = index | ||
} | ||
} | ||
|
||
func handleAppleIDCredential(_ credential: ASAuthorizationAppleIDCredential) { | ||
let fullName = credential.fullName | ||
let name = (fullName?.familyName ?? "") + (fullName?.givenName ?? "") | ||
UserManager.shared.userName = name | ||
guard let idToken = String(data: credential.identityToken ?? Data(), encoding: .utf8) else { return print("no idToken!!") } | ||
private func startImageTimer() { | ||
Timer.publish(every: 3.0, on: .main, in: .common) | ||
.autoconnect() | ||
.sink { [weak self] _ in | ||
self?.state.swipeImageIndex = ((self?.state.swipeImageIndex ?? 0) + 1) % 3 | ||
} | ||
.store(in: cancelBag) | ||
} | ||
|
||
public func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { | ||
print(error.localizedDescription) | ||
} | ||
} |
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
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