From 277327e401fcad4446d57e7cad813e1adfb94c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxixn2=E2=80=9D?= Date: Sun, 10 Nov 2024 19:05:55 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20::=20[#48]=20Edit=20Signin=20?= =?UTF-8?q?=ED=86=A0=ED=81=B0=20=EB=A7=8C=EB=A3=8C=EC=8B=9C=EA=B0=84=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SignupFeature/Sources/AuthViewModel.swift | 72 ++++++++----------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/Projects/App/Sources/Feature/SignupFeature/Sources/AuthViewModel.swift b/Projects/App/Sources/Feature/SignupFeature/Sources/AuthViewModel.swift index 6fd7ac3..c13cb96 100644 --- a/Projects/App/Sources/Feature/SignupFeature/Sources/AuthViewModel.swift +++ b/Projects/App/Sources/Feature/SignupFeature/Sources/AuthViewModel.swift @@ -93,53 +93,40 @@ public final class AuthViewModel: ObservableObject { case 200: let signInResponse = try result.map(SigninResponse.self) - KeyChain.shared.saveTokenWithExpiration( - key: Const.KeyChainKey.accessToken, - token: signInResponse.accessToken, - expiresIn: Double(signInResponse.accessTokenExpiresIn) ?? 3600 - ) - - KeyChain.shared.saveTokenWithExpiration( - key: Const.KeyChainKey.refreshToken, - token: signInResponse.refreshToken, - expiresIn: Double(signInResponse.refreshTokenExpiresIn) ?? 2592000 - ) + // 만료 시간을 현재로부터의 시간 대신 `Date(timeIntervalSince1970:)`으로 변환 + if let accessTokenExpiresIn = Double(signInResponse.accessTokenExpiresIn), + let refreshTokenExpiresIn = Double(signInResponse.refreshTokenExpiresIn) { + + let accessTokenExpirationDate = Date(timeIntervalSince1970: accessTokenExpiresIn) + let refreshTokenExpirationDate = Date(timeIntervalSince1970: refreshTokenExpiresIn) + + KeyChain.shared.saveTokenWithExpiration( + key: Const.KeyChainKey.accessToken, + token: signInResponse.accessToken, + expiresIn: accessTokenExpirationDate.timeIntervalSinceNow + ) + + KeyChain.shared.saveTokenWithExpiration( + key: Const.KeyChainKey.refreshToken, + token: signInResponse.refreshToken, + expiresIn: refreshTokenExpirationDate.timeIntervalSinceNow + ) + + // 만료 시간 확인을 위한 출력 + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" + dateFormatter.timeZone = TimeZone.current + + print("Access Token 만료 날짜: \(dateFormatter.string(from: accessTokenExpirationDate))") + print("Refresh Token 만료 날짜: \(dateFormatter.string(from: refreshTokenExpirationDate))") + } else { + print("만료 시간 변환 오류: accessTokenExpiresIn 또는 refreshTokenExpiresIn이 Double로 변환되지 않음.") + } DispatchQueue.main.async { completion(statusCode, signInResponse.accessToken, signInResponse.refreshToken) } - if let savedAccessToken = KeyChain.shared.read(key: Const.KeyChainKey.accessToken), - let savedRefreshToken = KeyChain.shared.read(key: Const.KeyChainKey.refreshToken) { - print("Access Token 저장됨: \(savedAccessToken)") - print("Refresh Token 저장됨: \(savedRefreshToken)") - - if let accessTokenExpiresIn = Double(signInResponse.accessTokenExpiresIn), - let refreshTokenExpiresIn = Double(signInResponse.refreshTokenExpiresIn) { - - let accessTokenExpirationDate = Date(timeIntervalSinceNow: accessTokenExpiresIn) - let refreshTokenExpirationDate = Date(timeIntervalSinceNow: refreshTokenExpiresIn) - - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" - dateFormatter.timeZone = TimeZone.current - - print("Access Token 만료 날짜: \(dateFormatter.string(from: accessTokenExpirationDate))") - print("Refresh Token 만료 날짜: \(dateFormatter.string(from: refreshTokenExpirationDate))") - } else { - print("만료 시간 변환 오류: accessTokenExpiresIn 또는 refreshTokenExpiresIn이 Double로 변환되지 않음.") - print("accessTokenExpiresIn: \(signInResponse.accessTokenExpiresIn), refreshTokenExpiresIn: \(signInResponse.refreshTokenExpiresIn)") - - let defaultDate = Date() - let dateFormatter = DateFormatter() - dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" - dateFormatter.timeZone = TimeZone.current - print("기본 만료 날짜 (현재 시간): \(dateFormatter.string(from: defaultDate))") - } - } else { - print("토큰 저장 실패: Keychain에서 값을 읽어올 수 없습니다.") - } - case 400: print("400ㅣ비밀번호가 일치하지 않습니다.") DispatchQueue.main.async { @@ -187,6 +174,7 @@ public final class AuthViewModel: ObservableObject { } + // MARK: - Sign Up func signUp(completion: @escaping (Bool) -> Void) { let params = SignupRequest(name: name, nickname: nickname, email: email, password: password, phoneNumber: phoneNumber)