Skip to content

Commit

Permalink
๐ŸŽจ :: [#48] Edit Signin ํ† ํฐ ๋งŒ๋ฃŒ์‹œ๊ฐ„ ์ €์žฅ๋ฐฉ์‹ ๋ณ€๊ฒฝ
Browse files Browse the repository at this point in the history
  • Loading branch information
Xixn2 committed Nov 10, 2024
1 parent d13c53b commit 277327e
Showing 1 changed file with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 277327e

Please sign in to comment.