Skip to content

Commit

Permalink
update TokenManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Bone-Ash committed Jul 26, 2024
1 parent 46d71f2 commit 774dcb3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
32 changes: 24 additions & 8 deletions Sources/GHG/Core/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ public struct KeychainManager {
/// - Author: GH
public static func saveAccessTokenToKeychain(_ accessToken: String) {
KeychainSwift().set(accessToken, forKey: "AccessToken")
}

/// 从 Keychain 中读取 Access Token
/// - Returns: accessToken(String)
///
/// - Author: GH
public static func getAccessTokenFromKeychain() -> String? {
return KeychainSwift().get("AccessToken")
#if DEBUG
print("Access Token: \(accessToken)")
print("---Saved successfully!---")
#endif
}

/// 保存 Refresh Token 至 Keychain
Expand All @@ -31,6 +27,18 @@ public struct KeychainManager {
/// - Author: GH
public static func saveRefreshTokenToKeychain(_ refreshToken: String) {
KeychainSwift().set(refreshToken, forKey: "RefreshToken")
#if DEBUG
print("Refresh Token: \(refreshToken)")
print("---Saved successfully!---")
#endif
}

/// 从 Keychain 中读取 Access Token
/// - Returns: accessToken(String)
///
/// - Author: GH
public static func getAccessTokenFromKeychain() -> String? {
return KeychainSwift().get("AccessToken")
}

/// 从 Keychain 中读取 Refresh Token
Expand All @@ -56,6 +64,10 @@ public struct KeychainManager {
/// - Author: GH
public static func saveDeviceTokenToKeychain(_ deviceToken: String) {
KeychainSwift().set(deviceToken, forKey: "DeviceToken")
#if DEBUG
print("Device Token: \(deviceToken)")
print("---Saved successfully!---")
#endif
}

/// 从 Keychain 中读取 Device Token
Expand All @@ -79,6 +91,10 @@ public struct KeychainManager {
/// - Author: GH
public static func saveUUID(_ UUID: String) {
KeychainSwift().set(UUID, forKey: "UUID")
#if DEBUG
print("UUID: \(UUID)")
print("---Saved successfully!---")
#endif
}

/// 从 Keychain 中读取 UUID
Expand Down
27 changes: 0 additions & 27 deletions Sources/GHG/Core/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,4 @@ public actor TokenManager {
// Token 刷新的实现代码
return ""
}

/// 调用 Keychain Manager 的方法
/// - Parameter data: JSON 实例,会将 data 中 accessToken 字段提取并保存
///
/// - Author: GH
public static func saveAccessToken(_ data: JSON) {
if let accessToken = data["accessToken"].string {
KeychainManager.saveAccessTokenToKeychain(accessToken)
#if DEBUG
print("Access Token: \(accessToken)")
print("---Saved successfully!---")
#endif
}
}
/// 调用 Keychain Manager 的方法
/// - Parameter data: JSON 实例,会将 data 中 refreshToken 字段提取并保存
///
/// - Author: GH
public static func saveRefreshToken(_ data: JSON) {
if let refreshToken = data["refreshToken"].string {
KeychainManager.saveRefreshTokenToKeychain(refreshToken)
#if DEBUG
print("Refresh Token: \(refreshToken)")
print("---Saved successfully!---")
#endif
}
}
}

0 comments on commit 774dcb3

Please sign in to comment.