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)