-
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
1f3b9f4
commit f1df98e
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
.../LoginFeature/Sources/Refactor_LoginFeature_Combine/Feature/LoginViewModel_Refactor.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,115 @@ | ||
// | ||
// LoginViewModel_refactore.swift | ||
// LoginFeatureInterface | ||
// | ||
// Created by Seonwoo Kim on 10/9/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import Core | ||
import DSKit | ||
import AuthenticationServices | ||
import KakaoSDKUser | ||
|
||
class LoginViewModel_Refactor: NSObject, ObservableObject { | ||
|
||
private var useCase: LoginUseCaseType | ||
private var cancelBag = CancelBag() | ||
|
||
@Published private(set) var state = State( | ||
loginStatus: .loginFailure | ||
) | ||
|
||
init(useCase: LoginUseCaseType) { | ||
self.useCase = useCase | ||
} | ||
|
||
enum Action { | ||
case kakaoLoginDidTap | ||
case appleLoginDidTap | ||
} | ||
|
||
struct State { | ||
var loginStatus: LoginResponseType | ||
} | ||
|
||
func send(action: Action) { | ||
switch action { | ||
case .kakaoLoginDidTap: | ||
handleKakaoLogin() | ||
case .appleLoginDidTap: | ||
handleAppleLogin() | ||
} | ||
} | ||
|
||
private func handleKakaoLogin() { | ||
if (UserApi.isKakaoTalkLoginAvailable()) { | ||
UserApi.shared.loginWithKakaoTalk { [weak self] (oauthToken, error) in | ||
if let error = error { | ||
print("Kakao login error: \(error)") | ||
return | ||
} | ||
if let oauthToken = oauthToken { | ||
let idToken = oauthToken.accessToken | ||
let token = "Bearer " + idToken | ||
self?.requestLoginWithSocialToken(platform: "KAKAO", token: token) | ||
} | ||
} | ||
} else { | ||
UserApi.shared.loginWithKakaoAccount { [weak self] (oauthToken, error) in | ||
if let error = error { | ||
print("Kakao account login error: \(error)") | ||
return | ||
} | ||
if let oauthToken = oauthToken { | ||
let idToken = oauthToken.accessToken | ||
let token = "Bearer " + idToken | ||
self?.requestLoginWithSocialToken(platform: "KAKAO", token: token) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func handleAppleLogin() { | ||
let request = ASAuthorizationAppleIDProvider().createRequest() | ||
request.requestedScopes = [.fullName, .email] | ||
|
||
let authorizationController = ASAuthorizationController(authorizationRequests: [request]) | ||
authorizationController.delegate = self | ||
authorizationController.performRequests() | ||
} | ||
|
||
private func requestLoginWithSocialToken(platform: String, token: String) { | ||
useCase.requestLogin(platform: platform, socialToken: token) | ||
.sink { _ in | ||
} receiveValue: {_ in | ||
bindLoginResponse() | ||
}.store(in: cancelBag) | ||
} | ||
|
||
private func bindLoginResponse() { | ||
useCase.loginResponse | ||
.sink(receiveCompletion: { _ in }, receiveValue: { [weak self] response in | ||
self?.state.loginStatus = response | ||
}).store(in: cancelBag) | ||
} | ||
} | ||
|
||
extension LoginViewModel_Refactor: ASAuthorizationControllerDelegate { | ||
|
||
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { | ||
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential { | ||
guard let identityToken = appleIDCredential.identityToken, | ||
let idTokenString = String(data: identityToken, encoding: .utf8) else { | ||
print("Failed to get Apple ID token") | ||
return | ||
} | ||
requestLoginWithSocialToken(platform: "APPLE", token: idTokenString) | ||
} | ||
} | ||
|
||
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { | ||
print("Apple login error: \(error.localizedDescription)") | ||
} | ||
} |