Skip to content

Commit

Permalink
[#73] 로그인 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
00yhsp committed Feb 12, 2024
1 parent 80ce0d1 commit 89a3c73
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
25 changes: 24 additions & 1 deletion Spon-us/Manager/KeyChainManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class KeychainManager {

// MARK: - Load

static func load(account: String) throws -> String {
static func loadString(account: String) throws -> String {
try String(decoding: load(account: account), as: UTF8.self)
}

Expand Down Expand Up @@ -122,3 +122,26 @@ class KeychainManager {
}
}
}


func saveAccessToken(userID: String, accessToken: String) {
let account = "\(userID)_accessToken"
try? KeychainManager.save(account: account, value: accessToken, isForce: true)
}

func saveRefreshToken(userID: String, refreshToken: String) {
let account = "\(userID)_refreshToken"
try? KeychainManager.save(account: account, value: refreshToken, isForce: true)
}

func loadAccessToken(userID: String) -> String {
let account = "\(userID)_accessToken"
let accessToken = (try? KeychainManager.loadString(account: account)) ?? "Access Token Loading Error"
return accessToken
}

func loadRefreshToken(userID: String) -> String {
let account = "\(userID)_refreshToken"
let accessToken = (try? KeychainManager.loadString(account: account)) ?? "Refresh Token Loading Error"
return accessToken
}
1 change: 1 addition & 0 deletions Spon-us/Model/Onboarding/LoginViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LoginViewModel: ObservableObject {
let loginResponse = try response.map(LoginModel201.self)
self.login201 = loginResponse
self.isBadRequest = false
print(self.login201)
completion(true)
}
else {
Expand Down
7 changes: 6 additions & 1 deletion Spon-us/Spon_usApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ struct Spon_usApp: App {
@State var rootIsActive = true
var body: some Scene {
WindowGroup {
OnBoardingView()
if let userID = UserDefaults.standard.string(forKey: "loginAccount") {
ContentView()
}
else {
OnBoardingView()
}
// ContentView()
// ChargerInfoViewTest(rootIsActive: $rootIsActive)
}
Expand Down
4 changes: 3 additions & 1 deletion Spon-us/View/MyPage/MyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ struct MyView: View {
),
secondaryButton: .default(
Text("로그아웃"),
action: {}
action: {
UserDefaults.standard.removeObject(forKey: "loginAccount")
}
)
)
}
Expand Down
10 changes: 8 additions & 2 deletions Spon-us/View/Onboarding/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,14 @@ struct LoginView: View {
print("FCM registration token: \(token)")
loginViewModel.postLogin(email: userID, password: userPW, fcmToken: token) { success in
if success {
print("access token : \n\(String(describing: loginViewModel.login201?.content.accessToken))")
print("refresh token : \n\(String(describing: loginViewModel.login201?.content.refreshToken))")
// 로그인한 유저의 이메일 정보 -> 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 {
Expand Down
1 change: 1 addition & 0 deletions Spon-us/View/Onboarding/SelectUserTypeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ struct OnboardingCompletedView: View {
}.padding(.top, 12)
Spacer()
Button {
UserDefaults.standard.set(userID, forKey: "loginAccount")
goToContentView = true
} label: {
Text("시작하기").font(.Body04).frame(maxWidth: .infinity).frame(height: 56).foregroundStyle(.sponusWhite).background(joinViewModel.isButtonEnabled ? .sponusPrimary : .sponusGrey600).padding(.bottom, 16)
Expand Down

0 comments on commit 89a3c73

Please sign in to comment.