From 01d5ea1fe4460eed9c9e758708043e739f053e58 Mon Sep 17 00:00:00 2001 From: hooni Date: Mon, 15 Jul 2024 13:40:36 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat/#178=20=EC=86=8C=EC=85=9C=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KkuMulKum.xcodeproj/project.pbxproj | 14 +++ .../Network/DTO/TargetType/LoginService.swift | 96 +++++++++++++++++++ .../Login/VIewModel/LoginViewModel.swift | 86 ++++++++++++----- .../ViewController/LoginViewController.swift | 49 +++++++--- 4 files changed, 205 insertions(+), 40 deletions(-) create mode 100644 KkuMulKum/Network/DTO/TargetType/LoginService.swift diff --git a/KkuMulKum.xcodeproj/project.pbxproj b/KkuMulKum.xcodeproj/project.pbxproj index 95951034..2eba4399 100644 --- a/KkuMulKum.xcodeproj/project.pbxproj +++ b/KkuMulKum.xcodeproj/project.pbxproj @@ -62,6 +62,7 @@ 78B928752C29402E006D9942 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 78B928742C29402E006D9942 /* Assets.xcassets */; }; 78B928782C29402E006D9942 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 78B928762C29402E006D9942 /* LaunchScreen.storyboard */; }; 78BD61202C43F557005752FD /* SwiftKeychainWrapper in Frameworks */ = {isa = PBXBuildFile; productRef = 78BD611F2C43F557005752FD /* SwiftKeychainWrapper */; }; + 78BD61272C446A97005752FD /* LoginService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78BD61262C446A97005752FD /* LoginService.swift */; }; A3DD9C3D2C41BAD000E58A13 /* MeetingTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C322C41BAD000E58A13 /* MeetingTableViewCell.swift */; }; A3DD9C3E2C41BAD000E58A13 /* MeetingDummyModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C342C41BAD000E58A13 /* MeetingDummyModel.swift */; }; A3DD9C3F2C41BAD000E58A13 /* MeetingListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C362C41BAD000E58A13 /* MeetingListView.swift */; }; @@ -196,6 +197,7 @@ 78B928742C29402E006D9942 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 78B928772C29402E006D9942 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 78B928792C29402E006D9942 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 78BD61262C446A97005752FD /* LoginService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginService.swift; sourceTree = ""; }; A3DD9C322C41BAD000E58A13 /* MeetingTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingTableViewCell.swift; sourceTree = ""; }; A3DD9C342C41BAD000E58A13 /* MeetingDummyModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingDummyModel.swift; sourceTree = ""; }; A3DD9C362C41BAD000E58A13 /* MeetingListView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingListView.swift; sourceTree = ""; }; @@ -508,6 +510,14 @@ path = KkuMulKum; sourceTree = ""; }; + 78BD61252C446A84005752FD /* TargetType */ = { + isa = PBXGroup; + children = ( + 78BD61262C446A97005752FD /* LoginService.swift */, + ); + path = TargetType; + sourceTree = ""; + }; A3DD9C332C41BAD000E58A13 /* Cell */ = { isa = PBXGroup; children = ( @@ -1111,6 +1121,7 @@ DE9E18852C3BC8F000DB76B4 /* DTO */ = { isa = PBXGroup; children = ( + 78BD61252C446A84005752FD /* TargetType */, DE9E18872C3BC90300DB76B4 /* Model */, DE9E18862C3BC8F900DB76B4 /* ResponseBody */, ); @@ -1393,6 +1404,7 @@ A3FB184F2C3BF4BC001483E5 /* MakeMeetingsResponseModel.swift in Sources */, DD3976852C41C2AD00E2A4C4 /* UpcomingPromiseModel.swift in Sources */, DEF725DB2C3F3BBF008C87C7 /* Toast.swift in Sources */, + 78BD61272C446A97005752FD /* LoginService.swift in Sources */, DD43937A2C412F4500EC1799 /* FinishCreateViewController.swift in Sources */, DE254AAC2C31192400A4015E /* UILabel+.swift in Sources */, DE254AB72C3119D000A4015E /* ReuseIdentifiable.swift in Sources */, @@ -1543,6 +1555,7 @@ LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; + NEW_SETTING = ""; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; @@ -1600,6 +1613,7 @@ LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; + NEW_SETTING = ""; SDKROOT = iphoneos; SWIFT_COMPILATION_MODE = wholemodule; VALIDATE_PRODUCT = YES; diff --git a/KkuMulKum/Network/DTO/TargetType/LoginService.swift b/KkuMulKum/Network/DTO/TargetType/LoginService.swift new file mode 100644 index 00000000..6ba0227e --- /dev/null +++ b/KkuMulKum/Network/DTO/TargetType/LoginService.swift @@ -0,0 +1,96 @@ +// +// LoginService.swift +// KkuMulKum +// +// Created by 이지훈 on 7/15/24. +// +import Foundation +import Moya + +enum LoginService { + case appleLogin(identityToken: String, fcmToken: String) + case kakaoLogin(accessToken: String, fcmToken: String) +} + +extension LoginService: TargetType { + var baseURL: URL { + print("Attempting to get baseURL") + guard let privacyInfo = Bundle.main.privacyInfo else { + print("PrivacyInfo.plist not found") + fatalError("PrivacyInfo.plist not found") + } + + guard let urlString = privacyInfo["BASE_URL"] as? String else { + print("BASE_URL not found in PrivacyInfo.plist") + fatalError("BASE_URL not found in PrivacyInfo.plist") + } + + print("BASE_URL from PrivacyInfo.plist: \(urlString)") + + guard let url = URL(string: urlString) else { + print("Invalid BASE_URL in PrivacyInfo.plist: \(urlString)") + fatalError("Invalid BASE_URL in PrivacyInfo.plist: \(urlString)") + } + + print("Successfully created URL: \(url)") + return url + } + + var path: String { + print("Getting path: /api/v1/auth/signin") + return "/api/v1/auth/signin" + } + + var method: Moya.Method { + print("HTTP Method: POST") + return .post + } + + var task: Task { + switch self { + case let .appleLogin(_, fcmToken): + print("Creating task for Apple Login with fcmToken: \(fcmToken)") + return .requestParameters( + parameters: ["provider": "APPLE", "fcmToken": fcmToken], + encoding: JSONEncoding.default + ) + case let .kakaoLogin(_, fcmToken): + print("Creating task for Kakao Login with fcmToken: \(fcmToken)") + return .requestParameters( + parameters: ["provider": "KAKAO", "fcmToken": fcmToken], + encoding: JSONEncoding.default + ) + } + } + + var headers: [String : String]? { + switch self { + case .appleLogin(let identityToken, _): + print("Setting headers for Apple Login") + return ["Authorization": identityToken, "Content-Type": "application/json"] + case .kakaoLogin(let accessToken, _): + print("Setting headers for Kakao Login") + return ["Authorization": accessToken, "Content-Type": "application/json"] + } + } +} + +extension Bundle { + var privacyInfo: [String: Any]? { + print("Attempting to read PrivacyInfo.plist") + guard let url = self.url(forResource: "PrivacyInfo", withExtension: "plist") else { + print("PrivacyInfo.plist file not found") + return nil + } + guard let data = try? Data(contentsOf: url) else { + print("Failed to read data from PrivacyInfo.plist") + return nil + } + guard let result = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] else { + print("Failed to serialize PrivacyInfo.plist") + return nil + } + print("Successfully read PrivacyInfo.plist") + return result + } +} diff --git a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift index cfb801eb..ec2870cf 100644 --- a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift +++ b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift @@ -4,24 +4,27 @@ // // Created by 이지훈 on 7/9/24. // - import UIKit import AuthenticationServices import KakaoSDKUser import KakaoSDKAuth - +import Moya enum LoginState { case notLoggedIn case loggedIn(userInfo: String) + case needOnboarding } class LoginViewModel: NSObject { var loginState: ObservablePattern = ObservablePattern(.notLoggedIn) var error: ObservablePattern = ObservablePattern("") + private let provider = MoyaProvider(plugins: [NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))]) + func performAppleLogin(presentationAnchor: ASPresentationAnchor) { + print("Performing Apple Login") let request = ASAuthorizationAppleIDProvider().createRequest() request.requestedScopes = [.fullName, .email] @@ -31,12 +34,15 @@ class LoginViewModel: NSObject { controller.performRequests() } - func performKakaoLogin(presentationAnchor: UIWindow) { + func performKakaoLogin() { + print("Performing Kakao Login") if UserApi.isKakaoTalkLoginAvailable() { + print("Kakao Talk is available, using Kakao Talk login") UserApi.shared.loginWithKakaoTalk { [weak self] (oauthToken, error) in self?.handleKakaoLoginResult(oauthToken: oauthToken, error: error) } } else { + print("Kakao Talk is not available, using Kakao Account login") UserApi.shared.loginWithKakaoAccount { [weak self] (oauthToken, error) in self?.handleKakaoLoginResult(oauthToken: oauthToken, error: error) } @@ -45,27 +51,59 @@ class LoginViewModel: NSObject { private func handleKakaoLoginResult(oauthToken: OAuthToken?, error: Error?) { if let error = error { + print("Kakao Login Error: \(error.localizedDescription)") self.error.value = error.localizedDescription return } - if let _ = oauthToken { - fetchKakaoUserInfo() + if let token = oauthToken?.accessToken { + print("Kakao Login Successful, access token: \(token)") + loginToServer(with: .kakaoLogin(accessToken: token, fcmToken: "dummy_fcm_token")) + } else { + print("Kakao Login Error: No access token") + self.error.value = "No access token received" } } - private func fetchKakaoUserInfo() { - UserApi.shared.me() { [weak self] (user, error) in - if let error = error { - self?.error.value = error.localizedDescription - return - } - - if let nickname = user?.kakaoAccount?.profile?.nickname { - self?.loginState.value = .loggedIn(userInfo: "Kakao user: \(nickname)") + private func loginToServer(with loginService: LoginService) { + print("Attempting to login to server") + provider.request(loginService) { [weak self] result in + switch result { + case .success(let response): + print("Received response from server: \(response)") + do { + let loginResponse = try response.map(SocialLoginResponseModel.self) + print("Successfully mapped response: \(loginResponse)") + self?.handleLoginResponse(loginResponse) + } catch { + print("Failed to decode response: \(error)") + self?.error.value = "Failed to decode response: \(error.localizedDescription)" + } + case .failure(let error): + print("Network error: \(error)") + self?.error.value = "Network error: \(error.localizedDescription)" } } } + + private func handleLoginResponse(_ response: SocialLoginResponseModel) { + print("Handling login response") + if let name = response.name { + print("Login successful, user name: \(name)") + loginState.value = .loggedIn(userInfo: name) + } else { + print("Login successful, but no name provided. Needs onboarding.") + loginState.value = .needOnboarding + } + + // TODO: 토큰 저장 + if let accessToken = response.accessToken, let refreshToken = response.refreshToken { + print("Received tokens - Access: \(accessToken), Refresh: \(refreshToken)") + // Save tokens to secure storage + } else { + print("Warning: No tokens received in response") + } + } } extension LoginViewModel: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding { @@ -73,32 +111,30 @@ extension LoginViewModel: ASAuthorizationControllerDelegate, ASAuthorizationCont controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization ) { - guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential else { - print("Authorization failed: Credential is not of type ASAuthorizationAppleIDCredential") + print("Apple authorization completed") + guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential, + let identityToken = appleIDCredential.identityToken, + let tokenString = String(data: identityToken, encoding: .utf8) else { + print("Failed to get Apple ID Credential or identity token") return } - let userName = appleIDCredential.fullName?.givenName ?? "Apple user" - loginState.value = .loggedIn(userInfo: "Apple user: \(userName)") - - /// 액세스 토큰 출력 - if let identityToken = appleIDCredential.identityToken, - let tokenString = String(data: identityToken, encoding: .utf8) { - print("Apple Login Access Token: \(tokenString)") - } + print("Apple Login Successful, identity token: \(tokenString)") + loginToServer(with: .appleLogin(identityToken: tokenString, fcmToken: "dummy_fcm_token")) } func authorizationController( controller: ASAuthorizationController, didCompleteWithError error: Error ) { + print("Apple authorization error: \(error.localizedDescription)") self.error.value = error.localizedDescription } func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor { + print("Providing presentation anchor for Apple Login") let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene let window = windowScene?.windows.first - return window! } } diff --git a/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift b/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift index 4be171fa..2fc3f78d 100644 --- a/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift +++ b/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift @@ -6,6 +6,7 @@ // import UIKit + import AuthenticationServices class LoginViewController: BaseViewController { @@ -15,16 +16,19 @@ class LoginViewController: BaseViewController { override func loadView() { view = loginView + print("LoginViewController loadView called") } override func viewDidLoad() { super.viewDidLoad() + print("LoginViewController viewDidLoad called") bindViewModel() setupAction() } override func setupAction() { super.setupAction() + print("Setting up actions for LoginViewController") let appleTapGesture = UITapGestureRecognizer( target: self, @@ -38,7 +42,6 @@ class LoginViewController: BaseViewController { ) loginView.kakaoLoginImageView.addGestureRecognizer(kakaoTapGesture) - /// 더미 버튼 loginView.dummyNextButton.addTarget( self, action: #selector(dummyNextButtonTapped), @@ -47,45 +50,61 @@ class LoginViewController: BaseViewController { } private func bindViewModel() { + print("Binding ViewModel in LoginViewController") loginViewModel.loginState.bind(with: self) { owner, state in switch state { case .notLoggedIn: - print("Not logged in") + print("Login State: Not logged in") case .loggedIn(let userInfo): - print("Logged in: \(userInfo)") + print("Login State: Logged in with user info: \(userInfo)") + owner.navigateToMainScreen() + case .needOnboarding: + print("Login State: Need onboarding") + owner.navigateToOnboardingScreen() } } loginViewModel.error.bind(with: self) { owner, error in if !error.isEmpty { - // TODO: 추후 에러처리 추가예정 -> Keychain 연결 이후 - print("Error occurred: \(error)") + print("Login Error: \(error)") + owner.showErrorAlert(message: error) } } } @objc private func appleLoginTapped() { + print("Apple Login button tapped") loginViewModel.performAppleLogin(presentationAnchor: view.window!) } @objc private func kakaoLoginTapped() { - loginViewModel.performKakaoLogin(presentationAnchor: view.window!) + print("Kakao Login button tapped") + loginViewModel.performKakaoLogin() } - // TODO: 추후 서버연결후 삭제예정 @objc private func dummyNextButtonTapped() { -// _ = NicknameViewController() -// let welcomeViewController = NicknameViewController() -// welcomeViewController.modalPresentationStyle = .fullScreen -// present(welcomeViewController, animated: true, completion: nil) - - // TODO: 프로필 설정부터 네비게이션으로 플로우 동작 - + print("Dummy Next button tapped") let viewController = NicknameViewController() let navigationController = UINavigationController(rootViewController: viewController) navigationController.modalTransitionStyle = .crossDissolve navigationController.modalPresentationStyle = .fullScreen present(navigationController, animated: true) } + + private func navigateToMainScreen() { + print("Navigating to Main Screen") + // TODO: Implement navigation to main screen + } + + private func navigateToOnboardingScreen() { + print("Navigating to Onboarding Screen") + // TODO: Implement navigation to onboarding screen + } + + private func showErrorAlert(message: String) { + print("Showing error alert with message: \(message)") + let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "OK", style: .default)) + present(alert, animated: true) + } } - From 24e384e5ce785e8bdbed306bfb4f2ec94df58829 Mon Sep 17 00:00:00 2001 From: hooni Date: Mon, 15 Jul 2024 16:40:31 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat/#178=20=EC=86=8C=EC=85=9C=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=ED=99=94=EB=A9=B4=EC=A0=84=ED=99=98=20?= =?UTF-8?q?=EB=B6=84=EA=B8=B0=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 자잘한 주석및 컨벤션 수정 --- .../Model/Auth/SocialLoginRequestModel.swift | 2 + .../Model/Auth/SocialLoginResponseModel.swift | 19 +++++- .../Network/DTO/TargetType/LoginService.swift | 24 ++----- .../Login/VIewModel/LoginViewModel.swift | 65 ++++++++++++------- .../ViewController/LoginViewController.swift | 36 ++++++---- 5 files changed, 91 insertions(+), 55 deletions(-) diff --git a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginRequestModel.swift b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginRequestModel.swift index bb35bb9c..71f83e0b 100644 --- a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginRequestModel.swift +++ b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginRequestModel.swift @@ -9,4 +9,6 @@ import Foundation struct SocialLoginRequestModel: RequestModelType { let provider: String? + let fcmToken: String + } diff --git a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift index c7ca4889..a2837e58 100644 --- a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift +++ b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift @@ -8,5 +8,22 @@ import Foundation struct SocialLoginResponseModel: ResponseModelType { - let name, accessToken, refreshToken: String? + let success: Bool + let data: LoginData? + let error: ErrorData? +} + +struct LoginData: Codable { + let name: String? + let jwtTokenDto: JwtTokenDto +} + +struct JwtTokenDto: Codable { + let accessToken: String + let refreshToken: String +} + +struct ErrorData: Codable { + let code: Int + let message: String } diff --git a/KkuMulKum/Network/DTO/TargetType/LoginService.swift b/KkuMulKum/Network/DTO/TargetType/LoginService.swift index 6ba0227e..468135ff 100644 --- a/KkuMulKum/Network/DTO/TargetType/LoginService.swift +++ b/KkuMulKum/Network/DTO/TargetType/LoginService.swift @@ -16,23 +16,17 @@ extension LoginService: TargetType { var baseURL: URL { print("Attempting to get baseURL") guard let privacyInfo = Bundle.main.privacyInfo else { - print("PrivacyInfo.plist not found") fatalError("PrivacyInfo.plist not found") } guard let urlString = privacyInfo["BASE_URL"] as? String else { - print("BASE_URL not found in PrivacyInfo.plist") fatalError("BASE_URL not found in PrivacyInfo.plist") } - - print("BASE_URL from PrivacyInfo.plist: \(urlString)") - + guard let url = URL(string: urlString) else { - print("Invalid BASE_URL in PrivacyInfo.plist: \(urlString)") fatalError("Invalid BASE_URL in PrivacyInfo.plist: \(urlString)") } - print("Successfully created URL: \(url)") return url } @@ -42,20 +36,17 @@ extension LoginService: TargetType { } var method: Moya.Method { - print("HTTP Method: POST") return .post } var task: Task { switch self { case let .appleLogin(_, fcmToken): - print("Creating task for Apple Login with fcmToken: \(fcmToken)") return .requestParameters( parameters: ["provider": "APPLE", "fcmToken": fcmToken], encoding: JSONEncoding.default ) case let .kakaoLogin(_, fcmToken): - print("Creating task for Kakao Login with fcmToken: \(fcmToken)") return .requestParameters( parameters: ["provider": "KAKAO", "fcmToken": fcmToken], encoding: JSONEncoding.default @@ -66,10 +57,8 @@ extension LoginService: TargetType { var headers: [String : String]? { switch self { case .appleLogin(let identityToken, _): - print("Setting headers for Apple Login") return ["Authorization": identityToken, "Content-Type": "application/json"] case .kakaoLogin(let accessToken, _): - print("Setting headers for Kakao Login") return ["Authorization": accessToken, "Content-Type": "application/json"] } } @@ -77,20 +66,19 @@ extension LoginService: TargetType { extension Bundle { var privacyInfo: [String: Any]? { - print("Attempting to read PrivacyInfo.plist") guard let url = self.url(forResource: "PrivacyInfo", withExtension: "plist") else { - print("PrivacyInfo.plist file not found") return nil } guard let data = try? Data(contentsOf: url) else { - print("Failed to read data from PrivacyInfo.plist") return nil } - guard let result = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] else { - print("Failed to serialize PrivacyInfo.plist") + guard let result = try? PropertyListSerialization.propertyList( + from: data, + options: [], + format: nil + ) as? [String: Any] else { return nil } - print("Successfully read PrivacyInfo.plist") return result } } diff --git a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift index ec2870cf..cd2c06b4 100644 --- a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift +++ b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift @@ -20,9 +20,15 @@ enum LoginState { class LoginViewModel: NSObject { var loginState: ObservablePattern = ObservablePattern(.notLoggedIn) var error: ObservablePattern = ObservablePattern("") - - private let provider = MoyaProvider(plugins: [NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))]) - + + private let provider = MoyaProvider( + plugins: [NetworkLoggerPlugin( + configuration: .init( + logOptions: .verbose + ) + )] + ) + func performAppleLogin(presentationAnchor: ASPresentationAnchor) { print("Performing Apple Login") let request = ASAuthorizationAppleIDProvider().createRequest() @@ -35,14 +41,13 @@ class LoginViewModel: NSObject { } func performKakaoLogin() { - print("Performing Kakao Login") if UserApi.isKakaoTalkLoginAvailable() { - print("Kakao Talk is available, using Kakao Talk login") + print("Kakao Talk is available") UserApi.shared.loginWithKakaoTalk { [weak self] (oauthToken, error) in self?.handleKakaoLoginResult(oauthToken: oauthToken, error: error) } } else { - print("Kakao Talk is not available, using Kakao Account login") + print("Kakao Talk is not available") UserApi.shared.loginWithKakaoAccount { [weak self] (oauthToken, error) in self?.handleKakaoLoginResult(oauthToken: oauthToken, error: error) } @@ -58,7 +63,7 @@ class LoginViewModel: NSObject { if let token = oauthToken?.accessToken { print("Kakao Login Successful, access token: \(token)") - loginToServer(with: .kakaoLogin(accessToken: token, fcmToken: "dummy_fcm_token")) + loginToServer(with: .kakaoLogin(accessToken: token, fcmToken: "")) } else { print("Kakao Login Error: No access token") self.error.value = "No access token received" @@ -66,7 +71,6 @@ class LoginViewModel: NSObject { } private func loginToServer(with loginService: LoginService) { - print("Attempting to login to server") provider.request(loginService) { [weak self] result in switch result { case .success(let response): @@ -79,6 +83,7 @@ class LoginViewModel: NSObject { print("Failed to decode response: \(error)") self?.error.value = "Failed to decode response: \(error.localizedDescription)" } + case .failure(let error): print("Network error: \(error)") self?.error.value = "Network error: \(error.localizedDescription)" @@ -88,25 +93,37 @@ class LoginViewModel: NSObject { private func handleLoginResponse(_ response: SocialLoginResponseModel) { print("Handling login response") - if let name = response.name { - print("Login successful, user name: \(name)") - loginState.value = .loggedIn(userInfo: name) - } else { - print("Login successful, but no name provided. Needs onboarding.") - loginState.value = .needOnboarding - } - - // TODO: 토큰 저장 - if let accessToken = response.accessToken, let refreshToken = response.refreshToken { - print("Received tokens - Access: \(accessToken), Refresh: \(refreshToken)") - // Save tokens to secure storage + if response.success { + if let data = response.data { + if let name = data.name { + print("Login successful, user name: \(name)") + loginState.value = .loggedIn(userInfo: name) + } else { + print("Login successful, but no name provided. Needs onboarding.") + loginState.value = .needOnboarding + } + + let tokens = data.jwtTokenDto + print("Received tokens - Access: \(tokens.accessToken), Refresh: \(tokens.refreshToken)") + // TODO: 키체인에 토큰 저장 로직추가 예정 + } else { + print("Warning: No data received in response") + error.value = "No data received" + } } else { - print("Warning: No tokens received in response") + if let error = response.error { + print("Login failed: \(error.message)") + self.error.value = error.message + } else { + print("Login failed: Unknown error") + self.error.value = "Unknown error occurred" + } } } } -extension LoginViewModel: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding { +extension LoginViewModel: ASAuthorizationControllerDelegate, + ASAuthorizationControllerPresentationContextProviding { func authorizationController( controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization @@ -120,9 +137,9 @@ extension LoginViewModel: ASAuthorizationControllerDelegate, ASAuthorizationCont } print("Apple Login Successful, identity token: \(tokenString)") - loginToServer(with: .appleLogin(identityToken: tokenString, fcmToken: "dummy_fcm_token")) + loginToServer(with: .appleLogin(identityToken: tokenString, fcmToken: "")) } - + func authorizationController( controller: ASAuthorizationController, didCompleteWithError error: Error diff --git a/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift b/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift index 2fc3f78d..3ee04ab9 100644 --- a/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift +++ b/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift @@ -16,19 +16,16 @@ class LoginViewController: BaseViewController { override func loadView() { view = loginView - print("LoginViewController loadView called") } override func viewDidLoad() { super.viewDidLoad() - print("LoginViewController viewDidLoad called") bindViewModel() setupAction() } override func setupAction() { super.setupAction() - print("Setting up actions for LoginViewController") let appleTapGesture = UITapGestureRecognizer( target: self, @@ -50,7 +47,6 @@ class LoginViewController: BaseViewController { } private func bindViewModel() { - print("Binding ViewModel in LoginViewController") loginViewModel.loginState.bind(with: self) { owner, state in switch state { case .notLoggedIn: @@ -73,17 +69,14 @@ class LoginViewController: BaseViewController { } @objc private func appleLoginTapped() { - print("Apple Login button tapped") loginViewModel.performAppleLogin(presentationAnchor: view.window!) } @objc private func kakaoLoginTapped() { - print("Kakao Login button tapped") loginViewModel.performKakaoLogin() } - + @objc private func dummyNextButtonTapped() { - print("Dummy Next button tapped") let viewController = NicknameViewController() let navigationController = UINavigationController(rootViewController: viewController) navigationController.modalTransitionStyle = .crossDissolve @@ -92,13 +85,32 @@ class LoginViewController: BaseViewController { } private func navigateToMainScreen() { - print("Navigating to Main Screen") - // TODO: Implement navigation to main screen + DispatchQueue.main.async { + let mainTabBarController = MainTabBarController() + + let navigationController = UINavigationController(rootViewController: mainTabBarController) + + navigationController.isNavigationBarHidden = true + navigationController.modalPresentationStyle = .fullScreen + navigationController.modalTransitionStyle = .crossDissolve + + self.present(navigationController, animated: true, completion: nil) + + } } private func navigateToOnboardingScreen() { - print("Navigating to Onboarding Screen") - // TODO: Implement navigation to onboarding screen + DispatchQueue.main.async { + let nicknameViewController = NicknameViewController() + if let navigationController = self.navigationController { + navigationController.pushViewController(nicknameViewController, animated: true) + } else { + let navigationController = UINavigationController(rootViewController: nicknameViewController) + navigationController.modalPresentationStyle = .fullScreen + navigationController.modalTransitionStyle = .crossDissolve + self.present(navigationController, animated: true, completion: nil) + } + } } private func showErrorAlert(message: String) { From f8c63566360d77d845e9c121db7d88659d2b1ac2 Mon Sep 17 00:00:00 2001 From: hooni Date: Mon, 15 Jul 2024 22:00:34 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor/#178=20=EC=84=9C=EB=B2=84=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=20=EB=B0=8F=20DTO=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KkuMulKum.xcodeproj/project.pbxproj | 4 ++ .../Model/Auth/SocialLoginRequestModel.swift | 3 +- .../Model/Auth/SocialLoginResponseModel.swift | 18 +++------ .../DTO/ResponseBody/ResponseBodyDTO.swift | 2 + .../Network/DTO/TargetType/LoginService.swift | 37 +++---------------- KkuMulKum/Resource/Bundle.swift | 23 ++++++++++++ .../Login/VIewModel/LoginViewModel.swift | 30 +++++++-------- .../ViewController/LoginViewController.swift | 25 +++++-------- 8 files changed, 66 insertions(+), 76 deletions(-) create mode 100644 KkuMulKum/Resource/Bundle.swift diff --git a/KkuMulKum.xcodeproj/project.pbxproj b/KkuMulKum.xcodeproj/project.pbxproj index 5ff740c3..4e5c75fb 100644 --- a/KkuMulKum.xcodeproj/project.pbxproj +++ b/KkuMulKum.xcodeproj/project.pbxproj @@ -63,6 +63,7 @@ 78B928782C29402E006D9942 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 78B928762C29402E006D9942 /* LaunchScreen.storyboard */; }; 78BD61202C43F557005752FD /* SwiftKeychainWrapper in Frameworks */ = {isa = PBXBuildFile; productRef = 78BD611F2C43F557005752FD /* SwiftKeychainWrapper */; }; 78BD61272C446A97005752FD /* LoginService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78BD61262C446A97005752FD /* LoginService.swift */; }; + 78BD612B2C4550A6005752FD /* Bundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78BD612A2C4550A6005752FD /* Bundle.swift */; }; A3DD9C3D2C41BAD000E58A13 /* MeetingTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C322C41BAD000E58A13 /* MeetingTableViewCell.swift */; }; A3DD9C3E2C41BAD000E58A13 /* MeetingDummyModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C342C41BAD000E58A13 /* MeetingDummyModel.swift */; }; A3DD9C3F2C41BAD000E58A13 /* MeetingListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C362C41BAD000E58A13 /* MeetingListView.swift */; }; @@ -205,6 +206,7 @@ 78B928772C29402E006D9942 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 78B928792C29402E006D9942 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 78BD61262C446A97005752FD /* LoginService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginService.swift; sourceTree = ""; }; + 78BD612A2C4550A6005752FD /* Bundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bundle.swift; sourceTree = ""; }; A3DD9C322C41BAD000E58A13 /* MeetingTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingTableViewCell.swift; sourceTree = ""; }; A3DD9C342C41BAD000E58A13 /* MeetingDummyModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingDummyModel.swift; sourceTree = ""; }; A3DD9C362C41BAD000E58A13 /* MeetingListView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingListView.swift; sourceTree = ""; }; @@ -1053,6 +1055,7 @@ DE254AA62C3118B300A4015E /* Extension */, DE254AB52C3119BC00A4015E /* Util */, DE254ABA2C311C3100A4015E /* Font */, + 78BD612A2C4550A6005752FD /* Bundle.swift */, ); path = Resource; sourceTree = ""; @@ -1540,6 +1543,7 @@ DD43937B2C412F4500EC1799 /* CreateMeetingViewController.swift in Sources */, DE8247FD2C36E7C7000601BC /* MoyaLoggingPlugin.swift in Sources */, DDAF1C842C3D5D19008A37D3 /* ViewModelType.swift in Sources */, + 78BD612B2C4550A6005752FD /* Bundle.swift in Sources */, DD3072262C3C0F0B00416D9F /* TardyInfoModel.swift in Sources */, DE254AB92C311AB300A4015E /* Screen.swift in Sources */, ); diff --git a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginRequestModel.swift b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginRequestModel.swift index 71f83e0b..cd4074dd 100644 --- a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginRequestModel.swift +++ b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginRequestModel.swift @@ -8,7 +8,6 @@ import Foundation struct SocialLoginRequestModel: RequestModelType { - let provider: String? + let provider: String let fcmToken: String - } diff --git a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift index a2837e58..61acb79b 100644 --- a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift +++ b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift @@ -7,23 +7,17 @@ import Foundation -struct SocialLoginResponseModel: ResponseModelType { - let success: Bool - let data: LoginData? - let error: ErrorData? -} - -struct LoginData: Codable { +struct UserData: ResponseModelType { let name: String? let jwtTokenDto: JwtTokenDto + + enum CodingKeys: String, CodingKey { + case name + case jwtTokenDto // 서버 응답과 + } } struct JwtTokenDto: Codable { let accessToken: String let refreshToken: String } - -struct ErrorData: Codable { - let code: Int - let message: String -} diff --git a/KkuMulKum/Network/DTO/ResponseBody/ResponseBodyDTO.swift b/KkuMulKum/Network/DTO/ResponseBody/ResponseBodyDTO.swift index b3347ed2..e07306af 100644 --- a/KkuMulKum/Network/DTO/ResponseBody/ResponseBodyDTO.swift +++ b/KkuMulKum/Network/DTO/ResponseBody/ResponseBodyDTO.swift @@ -9,6 +9,7 @@ import Foundation /// 제네릭 ResponseBody 구조체 정의 struct ResponseBodyDTO: Codable { + let success: Bool let data: T? let error: ErrorResponse? } @@ -18,3 +19,4 @@ struct ErrorResponse: Codable { let code: Int let message: String } + diff --git a/KkuMulKum/Network/DTO/TargetType/LoginService.swift b/KkuMulKum/Network/DTO/TargetType/LoginService.swift index 468135ff..28817bcd 100644 --- a/KkuMulKum/Network/DTO/TargetType/LoginService.swift +++ b/KkuMulKum/Network/DTO/TargetType/LoginService.swift @@ -4,6 +4,7 @@ // // Created by 이지훈 on 7/15/24. // + import Foundation import Moya @@ -14,24 +15,15 @@ enum LoginService { extension LoginService: TargetType { var baseURL: URL { - print("Attempting to get baseURL") - guard let privacyInfo = Bundle.main.privacyInfo else { - fatalError("PrivacyInfo.plist not found") - } - - guard let urlString = privacyInfo["BASE_URL"] as? String else { - fatalError("BASE_URL not found in PrivacyInfo.plist") - } - - guard let url = URL(string: urlString) else { - fatalError("Invalid BASE_URL in PrivacyInfo.plist: \(urlString)") + guard let privacyInfo = Bundle.main.privacyInfo, + let urlString = privacyInfo["BASE_URL"] as? String, + let url = URL(string: urlString) else { + fatalError("Invalid BASE_URL in PrivacyInfo.plist") } - return url } var path: String { - print("Getting path: /api/v1/auth/signin") return "/api/v1/auth/signin" } @@ -63,22 +55,3 @@ extension LoginService: TargetType { } } } - -extension Bundle { - var privacyInfo: [String: Any]? { - guard let url = self.url(forResource: "PrivacyInfo", withExtension: "plist") else { - return nil - } - guard let data = try? Data(contentsOf: url) else { - return nil - } - guard let result = try? PropertyListSerialization.propertyList( - from: data, - options: [], - format: nil - ) as? [String: Any] else { - return nil - } - return result - } -} diff --git a/KkuMulKum/Resource/Bundle.swift b/KkuMulKum/Resource/Bundle.swift new file mode 100644 index 00000000..33200100 --- /dev/null +++ b/KkuMulKum/Resource/Bundle.swift @@ -0,0 +1,23 @@ +// +// Bundle.swift +// KkuMulKum +// +// Created by 이지훈 on 7/15/24. +// + +import Foundation + +extension Bundle { + var privacyInfo: [String: Any]? { + guard let url = self.url(forResource: "PrivacyInfo", withExtension: "plist"), + let data = try? Data(contentsOf: url), + let result = try? PropertyListSerialization.propertyList( + from: data, + options: [], + format: nil + ) as? [String: Any] else { + return nil + } + return result + } +} diff --git a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift index cd2c06b4..4484d67f 100644 --- a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift +++ b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift @@ -4,9 +4,10 @@ // // Created by 이지훈 on 7/9/24. // -import UIKit +import UIKit import AuthenticationServices + import KakaoSDKUser import KakaoSDKAuth import Moya @@ -21,13 +22,12 @@ class LoginViewModel: NSObject { var loginState: ObservablePattern = ObservablePattern(.notLoggedIn) var error: ObservablePattern = ObservablePattern("") - private let provider = MoyaProvider( - plugins: [NetworkLoggerPlugin( - configuration: .init( - logOptions: .verbose - ) - )] - ) + private let provider: MoyaProvider + + init(provider: MoyaProvider = MoyaProvider(plugins: [NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))])) { + self.provider = provider + super.init() + } func performAppleLogin(presentationAnchor: ASPresentationAnchor) { print("Performing Apple Login") @@ -63,7 +63,7 @@ class LoginViewModel: NSObject { if let token = oauthToken?.accessToken { print("Kakao Login Successful, access token: \(token)") - loginToServer(with: .kakaoLogin(accessToken: token, fcmToken: "")) + loginToServer(with: .kakaoLogin(accessToken: token, fcmToken: "dummy_fcm_token")) } else { print("Kakao Login Error: No access token") self.error.value = "No access token received" @@ -76,7 +76,7 @@ class LoginViewModel: NSObject { case .success(let response): print("Received response from server: \(response)") do { - let loginResponse = try response.map(SocialLoginResponseModel.self) + let loginResponse = try response.map(ResponseBodyDTO.self) print("Successfully mapped response: \(loginResponse)") self?.handleLoginResponse(loginResponse) } catch { @@ -91,7 +91,7 @@ class LoginViewModel: NSObject { } } - private func handleLoginResponse(_ response: SocialLoginResponseModel) { + private func handleLoginResponse(_ response: ResponseBodyDTO) { print("Handling login response") if response.success { if let data = response.data { @@ -105,7 +105,7 @@ class LoginViewModel: NSObject { let tokens = data.jwtTokenDto print("Received tokens - Access: \(tokens.accessToken), Refresh: \(tokens.refreshToken)") - // TODO: 키체인에 토큰 저장 로직추가 예정 + // TODO: 토큰 저장 로직 구현 } else { print("Warning: No data received in response") error.value = "No data received" @@ -120,10 +120,10 @@ class LoginViewModel: NSObject { } } } + } -extension LoginViewModel: ASAuthorizationControllerDelegate, - ASAuthorizationControllerPresentationContextProviding { +extension LoginViewModel: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding { func authorizationController( controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization @@ -137,7 +137,7 @@ extension LoginViewModel: ASAuthorizationControllerDelegate, } print("Apple Login Successful, identity token: \(tokenString)") - loginToServer(with: .appleLogin(identityToken: tokenString, fcmToken: "")) + loginToServer(with: .appleLogin(identityToken: tokenString, fcmToken: "dummy_fcm_token")) } func authorizationController( diff --git a/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift b/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift index 21286186..228e1104 100644 --- a/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift +++ b/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift @@ -6,13 +6,21 @@ // import UIKit - import AuthenticationServices class LoginViewController: BaseViewController { private let loginView = LoginView() - private let loginViewModel = LoginViewModel() + private let loginViewModel: LoginViewModel + + init(viewModel: LoginViewModel = LoginViewModel()) { + self.loginViewModel = viewModel + super.init(nibName: nil, bundle: nil) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } override func loadView() { view = loginView @@ -77,32 +85,19 @@ class LoginViewController: BaseViewController { } @objc private func dummyNextButtonTapped() { -// _ = NicknameViewController() -// let welcomeViewController = NicknameViewController() -// welcomeViewController.modalPresentationStyle = .fullScreen -// present(welcomeViewController, animated: true, completion: nil) - - // TODO: 프로필 설정부터 네비게이션으로 플로우 동작 - let viewController = MainTabBarController() - viewController.modalPresentationStyle = .fullScreen - present(viewController, animated: true) } private func navigateToMainScreen() { DispatchQueue.main.async { let mainTabBarController = MainTabBarController() - let navigationController = UINavigationController(rootViewController: mainTabBarController) - navigationController.isNavigationBarHidden = true navigationController.modalPresentationStyle = .fullScreen navigationController.modalTransitionStyle = .crossDissolve - self.present(navigationController, animated: true, completion: nil) - } } From 241a5397d185e9cd1eb110183d8be257e4dc032b Mon Sep 17 00:00:00 2001 From: hooni Date: Mon, 15 Jul 2024 22:19:35 +0900 Subject: [PATCH 4/5] =?UTF-8?q?fix/#178=20=EC=BD=94=EB=93=9C=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KkuMulKum.xcodeproj/project.pbxproj | 18 +++-- .../Model/Auth/SocialLoginResponseModel.swift | 2 +- ...ginService.swift => LoginTargetType.swift} | 5 +- KkuMulKum/Resource/{ => Bundle}/Bundle.swift | 0 .../Login/VIewModel/LoginViewModel.swift | 68 +++++++++++-------- .../ViewController/LoginViewController.swift | 4 +- 6 files changed, 57 insertions(+), 40 deletions(-) rename KkuMulKum/Network/DTO/TargetType/{LoginService.swift => LoginTargetType.swift} (95%) rename KkuMulKum/Resource/{ => Bundle}/Bundle.swift (100%) diff --git a/KkuMulKum.xcodeproj/project.pbxproj b/KkuMulKum.xcodeproj/project.pbxproj index 4e5c75fb..b381a529 100644 --- a/KkuMulKum.xcodeproj/project.pbxproj +++ b/KkuMulKum.xcodeproj/project.pbxproj @@ -62,7 +62,7 @@ 78B928752C29402E006D9942 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 78B928742C29402E006D9942 /* Assets.xcassets */; }; 78B928782C29402E006D9942 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 78B928762C29402E006D9942 /* LaunchScreen.storyboard */; }; 78BD61202C43F557005752FD /* SwiftKeychainWrapper in Frameworks */ = {isa = PBXBuildFile; productRef = 78BD611F2C43F557005752FD /* SwiftKeychainWrapper */; }; - 78BD61272C446A97005752FD /* LoginService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78BD61262C446A97005752FD /* LoginService.swift */; }; + 78BD61272C446A97005752FD /* LoginTargetType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78BD61262C446A97005752FD /* LoginTargetType.swift */; }; 78BD612B2C4550A6005752FD /* Bundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78BD612A2C4550A6005752FD /* Bundle.swift */; }; A3DD9C3D2C41BAD000E58A13 /* MeetingTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C322C41BAD000E58A13 /* MeetingTableViewCell.swift */; }; A3DD9C3E2C41BAD000E58A13 /* MeetingDummyModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DD9C342C41BAD000E58A13 /* MeetingDummyModel.swift */; }; @@ -205,7 +205,7 @@ 78B928742C29402E006D9942 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 78B928772C29402E006D9942 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 78B928792C29402E006D9942 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 78BD61262C446A97005752FD /* LoginService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginService.swift; sourceTree = ""; }; + 78BD61262C446A97005752FD /* LoginTargetType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginTargetType.swift; sourceTree = ""; }; 78BD612A2C4550A6005752FD /* Bundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bundle.swift; sourceTree = ""; }; A3DD9C322C41BAD000E58A13 /* MeetingTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingTableViewCell.swift; sourceTree = ""; }; A3DD9C342C41BAD000E58A13 /* MeetingDummyModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeetingDummyModel.swift; sourceTree = ""; }; @@ -529,11 +529,19 @@ 78BD61252C446A84005752FD /* TargetType */ = { isa = PBXGroup; children = ( - 78BD61262C446A97005752FD /* LoginService.swift */, + 78BD61262C446A97005752FD /* LoginTargetType.swift */, ); path = TargetType; sourceTree = ""; }; + 78BD612C2C455680005752FD /* Bundle */ = { + isa = PBXGroup; + children = ( + 78BD612A2C4550A6005752FD /* Bundle.swift */, + ); + path = Bundle; + sourceTree = ""; + }; A3DD9C332C41BAD000E58A13 /* Cell */ = { isa = PBXGroup; children = ( @@ -1042,6 +1050,7 @@ DE254AA32C31107C00A4015E /* Resource */ = { isa = PBXGroup; children = ( + 78BD612C2C455680005752FD /* Bundle */, DEBA03302C3C2972002ED8F2 /* ViewController.swift */, DD39768B2C41C36B00E2A4C4 /* Color.xcassets */, 78B928742C29402E006D9942 /* Assets.xcassets */, @@ -1055,7 +1064,6 @@ DE254AA62C3118B300A4015E /* Extension */, DE254AB52C3119BC00A4015E /* Util */, DE254ABA2C311C3100A4015E /* Font */, - 78BD612A2C4550A6005752FD /* Bundle.swift */, ); path = Resource; sourceTree = ""; @@ -1462,7 +1470,7 @@ A3FB184F2C3BF4BC001483E5 /* MakeMeetingsResponseModel.swift in Sources */, DD3976852C41C2AD00E2A4C4 /* UpcomingPromiseModel.swift in Sources */, DEF725DB2C3F3BBF008C87C7 /* Toast.swift in Sources */, - 78BD61272C446A97005752FD /* LoginService.swift in Sources */, + 78BD61272C446A97005752FD /* LoginTargetType.swift in Sources */, DD43937A2C412F4500EC1799 /* FinishCreateViewController.swift in Sources */, DE254AAC2C31192400A4015E /* UILabel+.swift in Sources */, DE254AB72C3119D000A4015E /* ReuseIdentifiable.swift in Sources */, diff --git a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift index 61acb79b..46e6c3dc 100644 --- a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift +++ b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift @@ -13,7 +13,7 @@ struct UserData: ResponseModelType { enum CodingKeys: String, CodingKey { case name - case jwtTokenDto // 서버 응답과 + case jwtTokenDto // 서버 응답맞춰 수정 } } diff --git a/KkuMulKum/Network/DTO/TargetType/LoginService.swift b/KkuMulKum/Network/DTO/TargetType/LoginTargetType.swift similarity index 95% rename from KkuMulKum/Network/DTO/TargetType/LoginService.swift rename to KkuMulKum/Network/DTO/TargetType/LoginTargetType.swift index 28817bcd..f5c09c89 100644 --- a/KkuMulKum/Network/DTO/TargetType/LoginService.swift +++ b/KkuMulKum/Network/DTO/TargetType/LoginTargetType.swift @@ -6,14 +6,15 @@ // import Foundation + import Moya -enum LoginService { +enum LoginTargetType { case appleLogin(identityToken: String, fcmToken: String) case kakaoLogin(accessToken: String, fcmToken: String) } -extension LoginService: TargetType { +extension LoginTargetType: TargetType { var baseURL: URL { guard let privacyInfo = Bundle.main.privacyInfo, let urlString = privacyInfo["BASE_URL"] as? String, diff --git a/KkuMulKum/Resource/Bundle.swift b/KkuMulKum/Resource/Bundle/Bundle.swift similarity index 100% rename from KkuMulKum/Resource/Bundle.swift rename to KkuMulKum/Resource/Bundle/Bundle.swift diff --git a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift index 4484d67f..64abe953 100644 --- a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift +++ b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift @@ -13,18 +13,26 @@ import KakaoSDKAuth import Moya enum LoginState { - case notLoggedIn - case loggedIn(userInfo: String) + case notLogin + case login(userInfo: String) case needOnboarding } class LoginViewModel: NSObject { - var loginState: ObservablePattern = ObservablePattern(.notLoggedIn) + var loginState: ObservablePattern = ObservablePattern(.notLogin) var error: ObservablePattern = ObservablePattern("") - private let provider: MoyaProvider + private let provider: MoyaProvider - init(provider: MoyaProvider = MoyaProvider(plugins: [NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))])) { + init( + provider: MoyaProvider = MoyaProvider( + plugins: [NetworkLoggerPlugin( + configuration: .init( + logOptions: .verbose + ) + )] + ) + ) { self.provider = provider super.init() } @@ -70,8 +78,8 @@ class LoginViewModel: NSObject { } } - private func loginToServer(with loginService: LoginService) { - provider.request(loginService) { [weak self] result in + private func loginToServer(with loginTarget: LoginTargetType) { + provider.request(loginTarget) { [weak self] result in switch result { case .success(let response): print("Received response from server: \(response)") @@ -92,34 +100,34 @@ class LoginViewModel: NSObject { } private func handleLoginResponse(_ response: ResponseBodyDTO) { - print("Handling login response") - if response.success { - if let data = response.data { - if let name = data.name { - print("Login successful, user name: \(name)") - loginState.value = .loggedIn(userInfo: name) + print("Handling login response") + if response.success { + if let data = response.data { + if let name = data.name { + print("Login successful, user name: \(name)") + loginState.value = .login(userInfo: name) + } else { + print("Login successful, but no name provided. Needs onboarding.") + loginState.value = .needOnboarding + } + + let tokens = data.jwtTokenDto + print("Received tokens - Access: \(tokens.accessToken), Refresh: \(tokens.refreshToken)") + // TODO: 토큰 저장 로직 구현 } else { - print("Login successful, but no name provided. Needs onboarding.") - loginState.value = .needOnboarding + print("Warning: No data received in response") + error.value = "No data received" } - - let tokens = data.jwtTokenDto - print("Received tokens - Access: \(tokens.accessToken), Refresh: \(tokens.refreshToken)") - // TODO: 토큰 저장 로직 구현 - } else { - print("Warning: No data received in response") - error.value = "No data received" - } - } else { - if let error = response.error { - print("Login failed: \(error.message)") - self.error.value = error.message } else { - print("Login failed: Unknown error") - self.error.value = "Unknown error occurred" + if let error = response.error { + print("Login failed: \(error.message)") + self.error.value = error.message + } else { + print("Login failed: Unknown error") + self.error.value = "Unknown error occurred" + } } } - } } diff --git a/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift b/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift index 228e1104..a6a86acc 100644 --- a/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift +++ b/KkuMulKum/Source/Onboarding/Login/ViewController/LoginViewController.swift @@ -57,9 +57,9 @@ class LoginViewController: BaseViewController { private func bindViewModel() { loginViewModel.loginState.bind(with: self) { owner, state in switch state { - case .notLoggedIn: + case .notLogin: print("Login State: Not logged in") - case .loggedIn(let userInfo): + case .login(let userInfo): print("Login State: Logged in with user info: \(userInfo)") owner.navigateToMainScreen() case .needOnboarding: From ddeaec31189ea117d8f513a283306e946bfc4a1d Mon Sep 17 00:00:00 2001 From: hooni Date: Mon, 15 Jul 2024 22:28:33 +0900 Subject: [PATCH 5/5] =?UTF-8?q?fix/#178=20DTO=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=BB=A8=EB=B2=A4=EC=85=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Network/DTO/Model/Auth/SocialLoginResponseModel.swift | 8 ++++---- .../Onboarding/Login/VIewModel/LoginViewModel.swift | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift index 46e6c3dc..37bbff1d 100644 --- a/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift +++ b/KkuMulKum/Network/DTO/Model/Auth/SocialLoginResponseModel.swift @@ -7,17 +7,17 @@ import Foundation -struct UserData: ResponseModelType { +struct SocialLoginResponseModel: ResponseModelType { let name: String? - let jwtTokenDto: JwtTokenDto + let jwtTokenDTO: JwtTokenDTO enum CodingKeys: String, CodingKey { case name - case jwtTokenDto // 서버 응답맞춰 수정 + case jwtTokenDTO = "jwtTokenDto" } } -struct JwtTokenDto: Codable { +struct JwtTokenDTO: Codable { let accessToken: String let refreshToken: String } diff --git a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift index 64abe953..5e874191 100644 --- a/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift +++ b/KkuMulKum/Source/Onboarding/Login/VIewModel/LoginViewModel.swift @@ -84,7 +84,7 @@ class LoginViewModel: NSObject { case .success(let response): print("Received response from server: \(response)") do { - let loginResponse = try response.map(ResponseBodyDTO.self) + let loginResponse = try response.map(ResponseBodyDTO.self) print("Successfully mapped response: \(loginResponse)") self?.handleLoginResponse(loginResponse) } catch { @@ -99,7 +99,7 @@ class LoginViewModel: NSObject { } } - private func handleLoginResponse(_ response: ResponseBodyDTO) { + private func handleLoginResponse(_ response: ResponseBodyDTO) { print("Handling login response") if response.success { if let data = response.data { @@ -111,7 +111,7 @@ class LoginViewModel: NSObject { loginState.value = .needOnboarding } - let tokens = data.jwtTokenDto + let tokens = data.jwtTokenDTO print("Received tokens - Access: \(tokens.accessToken), Refresh: \(tokens.refreshToken)") // TODO: 토큰 저장 로직 구현 } else {