From 8c9a3f1f1667dae35789179835bf365a08575d79 Mon Sep 17 00:00:00 2001 From: 00yhsp <00yhsp@naver.com> Date: Wed, 14 Feb 2024 19:48:52 +0900 Subject: [PATCH] =?UTF-8?q?[#73]=20=EC=9C=A0=EC=A0=80=20=EC=95=A1=EC=84=B8?= =?UTF-8?q?=EC=8A=A4=ED=86=A0=ED=81=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Model/Category/CategoryModelData.swift | 2 +- Spon-us/Model/Onboarding/LoginViewModel.swift | 2 +- Spon-us/Spon_usApp.swift | 2 +- Spon-us/SponusAPI.swift | 26 +++++++------------ .../View/Onboarding/SelectUserTypeView.swift | 26 ++++++++++++++++++- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/Spon-us/Model/Category/CategoryModelData.swift b/Spon-us/Model/Category/CategoryModelData.swift index 2c38b8c..e9347b1 100644 --- a/Spon-us/Model/Category/CategoryModelData.swift +++ b/Spon-us/Model/Category/CategoryModelData.swift @@ -15,7 +15,7 @@ class CategoryModelData: ObservableObject { @Published var isLoading = false @Published var categoryModelDatas: [CategoryContent] = [] - let provider = MoyaProvider() + let provider = MoyaProvider(plugins: [NetworkLoggerPlugin()]) func setAPIValue(category: String?, type: String?) { self.isLoading = true diff --git a/Spon-us/Model/Onboarding/LoginViewModel.swift b/Spon-us/Model/Onboarding/LoginViewModel.swift index c7a3758..d44a934 100644 --- a/Spon-us/Model/Onboarding/LoginViewModel.swift +++ b/Spon-us/Model/Onboarding/LoginViewModel.swift @@ -24,7 +24,7 @@ class LoginViewModel: ObservableObject { let loginResponse = try response.map(LoginModel201.self) self.login201 = loginResponse self.isBadRequest = false - print(self.login201) + print(loginResponse) completion(true) } else { diff --git a/Spon-us/Spon_usApp.swift b/Spon-us/Spon_usApp.swift index e5548aa..d7bbf17 100644 --- a/Spon-us/Spon_usApp.swift +++ b/Spon-us/Spon_usApp.swift @@ -74,7 +74,7 @@ struct Spon_usApp: App { @State var rootIsActive = true var body: some Scene { WindowGroup { - if let userID = UserDefaults.standard.string(forKey: "loginAccount") { + if UserDefaults.standard.string(forKey: "loginAccount") != nil { ContentView() } else { diff --git a/Spon-us/SponusAPI.swift b/Spon-us/SponusAPI.swift index bbb61a2..5b2fded 100644 --- a/Spon-us/SponusAPI.swift +++ b/Spon-us/SponusAPI.swift @@ -246,36 +246,28 @@ extension SponusAPI: TargetType { case .postLogin: return nil case .getMe: - return ["Authorization": KeychainSwift().get("accessToken") ?? "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZW1haWwiOiJzcG9udXNfc3R1ZGVudEBnbWFpbC5jb20iLCJhdXRoIjoiU1RVREVOVCIsImlhdCI6MTcwNzgxNzkwNCwiZXhwIjoxNzA4NDIyNzA0fQ.r1QRU91tLjvDbiPco3RBnapB4j4DsXmbn-D7c0yfU6E"] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] case .getSent: - return ["Authorization": KeychainSwift().get("accessToken") ?? "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZW1haWwiOiJzcG9udXNfc3R1ZGVudEBnbWFpbC5jb20iLCJhdXRoIjoiU1RVREVOVCIsImlhdCI6MTcwNzgxNzkwNCwiZXhwIjoxNzA4NDIyNzA0fQ.r1QRU91tLjvDbiPco3RBnapB4j4DsXmbn-D7c0yfU6E"] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] case .getProposalDetail: - return ["Authorization": KeychainSwift().get("accessToken") ?? "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZW1haWwiOiJzcG9udXNfc3R1ZGVudEBnbWFpbC5jb20iLCJhdXRoIjoiU1RVREVOVCIsImlhdCI6MTcwNzgxNzkwNCwiZXhwIjoxNzA4NDIyNzA0fQ.r1QRU91tLjvDbiPco3RBnapB4j4DsXmbn-D7c0yfU6E"] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] case .getOrganization: - return ["Authorization": KeychainSwift().get("accessToken") ?? "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZW1haWwiOiJzcG9udXNfc3R1ZGVudEBnbWFpbC5jb20iLCJhdXRoIjoiU1RVREVOVCIsImlhdCI6MTcwNzgxNzkwNCwiZXhwIjoxNzA4NDIyNzA0fQ.r1QRU91tLjvDbiPco3RBnapB4j4DsXmbn-D7c0yfU6E"] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] /* case .postLike: return ["Content-Type": "application/json", "Accept": "application/json", "atk": KeychainSwift().get("accessToken") ?? ""] */ case .postAnnouncement(title: let title, type: let type, category: let category, content: let content, images: let images): - return [ - "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwiZW1haWwiOiJzcG9udXNAZ21haWwuY29tIiwiYXV0aCI6IkNPTVBBTlkiLCJpYXQiOjE3MDc3NDcwMzMsImV4cCI6MTcwODM1MTgzM30.fuPOq7cbQys7PwM0td9AEd6kVGfAPpcn1lQWOvlDTDU" - ] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] case .getCategory(category: let category, type: let type): - return [ - "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwiZW1haWwiOiJzcG9udXNAZ21haWwuY29tIiwiYXV0aCI6IkNPTVBBTlkiLCJpYXQiOjE3MDc3NDcwMzMsImV4cCI6MTcwODM1MTgzM30.fuPOq7cbQys7PwM0td9AEd6kVGfAPpcn1lQWOvlDTDU" - ] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] case .getAnnouncement(announcementId: let announcementId): - return [ - "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyIiwiZW1haWwiOiJzcG9udXNAZ21haWwuY29tIiwiYXV0aCI6IkNPTVBBTlkiLCJpYXQiOjE3MDc3NDcwMzMsImV4cCI6MTcwODM1MTgzM30.fuPOq7cbQys7PwM0td9AEd6kVGfAPpcn1lQWOvlDTDU" - ] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] case .propose(title: let title, content: let content, announcementId: let announcementId, attachments: let attachments): - return [ - "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZW1haWwiOiJzcG9udXNfc3R1ZGVudEBnbWFpbC5jb20iLCJhdXRoIjoiU1RVREVOVCIsImlhdCI6MTcwNzgxNzkwNCwiZXhwIjoxNzA4NDIyNzA0fQ.r1QRU91tLjvDbiPco3RBnapB4j4DsXmbn-D7c0yfU6E" - ] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] case .getReceived: - return ["Authorization": KeychainSwift().get("accessToken") ?? "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiZW1haWwiOiJzcG9udXNfc3R1ZGVudEBnbWFpbC5jb20iLCJhdXRoIjoiU1RVREVOVCIsImlhdCI6MTcwNzgxNzkwNCwiZXhwIjoxNzA4NDIyNzA0fQ.r1QRU91tLjvDbiPco3RBnapB4j4DsXmbn-D7c0yfU6E"] + return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"] } } } diff --git a/Spon-us/View/Onboarding/SelectUserTypeView.swift b/Spon-us/View/Onboarding/SelectUserTypeView.swift index ca89fce..670d961 100644 --- a/Spon-us/View/Onboarding/SelectUserTypeView.swift +++ b/Spon-us/View/Onboarding/SelectUserTypeView.swift @@ -6,6 +6,7 @@ // import SwiftUI +import Firebase struct SelectUserTypeView: View { @State var userID: String @@ -156,6 +157,7 @@ struct fillInGroupNameView: View { struct OnboardingCompletedView: View { @StateObject var joinViewModel = JoinViewModel() + @StateObject var loginViewModel = LoginViewModel() @State var userID: String @State var userPW: String @@ -182,7 +184,29 @@ struct OnboardingCompletedView: View { Spacer() Button { UserDefaults.standard.set(userID, forKey: "loginAccount") - goToContentView = true + Messaging.messaging().token { token, error in + if let error = error { + print("Error fetching FCM registration token: \(error)") + } else if let token = token { + print("FCM registration token: \(token)") + loginViewModel.postLogin(email: userID, password: userPW, fcmToken: token) { success in + if success { + // 로그인한 유저의 이메일 정보 -> UserDefaults Key "loginAccount"로 저장 + UserDefaults.standard.set(userID, forKey: "loginAccount") + // 액세스토큰 / 리프레시토큰 -> 키체인에 [userID]_accessToken / [userID]_refreshToken account로 저장 + saveAccessToken(userID: userID, accessToken: loginViewModel.login201?.content.accessToken ?? "AccessToken Saving Error") + saveRefreshToken(userID: userID, refreshToken: loginViewModel.login201?.content.refreshToken ?? "RefreshToken Saving Error") + // 액세스토큰, 리프레시토큰 필요시 아래 메소드 호출 + // loadAccessToken(userID: userID) + // loadRefreshToken(userID: userID) + goToContentView = true + } + else { + print("401\n\(String(describing: loginViewModel.login401?.message))") + } + } + } + } } label: { Text("시작하기").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background(joinViewModel.isButtonEnabled ? .sponusPrimary : .sponusGrey600).padding(.bottom, 16) }.disabled(!joinViewModel.isButtonEnabled).fullScreenCover(isPresented: $goToContentView, content: {