Skip to content

Commit

Permalink
[#73] 유저 액세스토큰 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
00yhsp committed Feb 14, 2024
1 parent 54f9cc3 commit 8c9a3f1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Spon-us/Model/Category/CategoryModelData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CategoryModelData: ObservableObject {
@Published var isLoading = false
@Published var categoryModelDatas: [CategoryContent] = []

let provider = MoyaProvider<SponusAPI>()
let provider = MoyaProvider<SponusAPI>(plugins: [NetworkLoggerPlugin()])

func setAPIValue(category: String?, type: String?) {
self.isLoading = true
Expand Down
2 changes: 1 addition & 1 deletion Spon-us/Model/Onboarding/LoginViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Spon-us/Spon_usApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 9 additions & 17 deletions Spon-us/SponusAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))"]
}
}
}
26 changes: 25 additions & 1 deletion Spon-us/View/Onboarding/SelectUserTypeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftUI
import Firebase

struct SelectUserTypeView: View {
@State var userID: String
Expand Down Expand Up @@ -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
Expand All @@ -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: {
Expand Down

0 comments on commit 8c9a3f1

Please sign in to comment.