11 lines
2.8 KiB
Markdown
Raw Normal View History

2024-12-23 16:55:04 +00:00
{
"extension": ".swift",
"source": "import Foundation\n\npublic struct AuthToken: Hashable, Sendable {\n public let id: Int\n public let key: String\n public let expireTime: Date\n public let userId: Int\n \n public init(id: Int, key: String, expireTime: Date, userId: Int) {\n self.id = id\n self.key = key\n self.expireTime = expireTime\n self.userId = userId\n }\n \n public var isExpired: Bool {\n expireTime < Date()\n }\n}\n\npublic extension AuthToken {\n public struct CodingData: Codable {\n public struct Container: Codable {\n let auth_token: AuthToken.CodingData\n }\n \n public let id: Int\n public let key: String\n public let expire_time: Int\n public let user_id: Int\n }\n}\n\npublic extension AuthToken.CodingData {\n public var decoded: AuthToken {\n .init(\n id: id,\n key: key,\n expireTime: Date(timeIntervalSince1970: TimeInterval(expire_time)),\n userId: user_id\n )\n }\n}\n\npublic extension AuthToken {\n public var encoded: AuthToken.CodingData {\n .init(\n id: id,\n key: key,\n expire_time: Int(expireTime.timeIntervalSince1970),\n user_id: userId\n )\n }\n}\n",
"review": "## 8\n\n### Bugs\n- No explicit bugs identified within the code provided.\n\n### Optimizations\n- The `public` access modifier is unnecessary in extensions if the enclosing type is already public.\n- Consider using `DateFormatter` for handling date formats if the date string needs to be human-readable.\n- The naming convention for variable `expire_time` in the `CodingData` struct could be `camelCase` consistent with Swift's convention.\n\n### Good points\n- The use of `Codable` ensures easy conversion of data to and from JSON or other formats.\n- The code clearly defines a structure for an authentication token and encapsulates its properties well.\n- `isExpired` computed property provides a readable way to check expiration status.\n\n### Summary\nThe code provides a clear and efficient structure for handling authentication tokens. It uses Swift's features effectively, such as `Codable` for easy serialization and `extension` for organizing code. The implementation can benefit slightly from adhering to naming conventions and removing unnecessary access modifiers.\n\n### Open source alternatives\n- [JWT.swift](https://github.com/IBM-Swift/Swift-JWT) - A Swift library for JSON Web Tokens.\n- [Vapor's JWT Package](https://github.com/vapor/jwt) - Provides comprehensive JWT functionalities in Swift.",
"filename": "AuthToken.swift",
"path": "Sources/SwiftDevRant/Models/AuthToken.swift",
"directory": "Models",
"grade": 8,
"size": 1259,
"line_count": 55
}