Cely
Cely copied to clipboard
How to integrate expirable into CelyUser to manage different tokens?
@initFabian I'd like to get your opinion on how to add the following Expirable protocol so I can manage tokens.
https://github.com/AndrewSB/Expirable
This will enable us see which token expired or is expiring soon. So far following your example I don't know where I am able to enable the Expirable protocol
struct AuthUser: CelyUser {
enum Property: CelyProperty {
case username = "username"
case email = "email"
case access_token = "access_token"
case refresh_token = "refresh_token"
func securely() -> Bool {
switch self {
case .access_token, .refresh_token:
return true
default:
return false
}
}
func persisted() -> Bool {
switch self {
case .access_token, .refresh_token:
return true
default:
return false
}
}
func save(_ value: Any) {
Cely.save(value, forKey: rawValue, securely: securely(), persisted: persisted())
}
func get() -> Any? {
return Cely.get(key: rawValue)
}
}
}
// MARK: - Save/Get User Properties
extension AuthUser {
static func save(_ value: Any, as property: Property) {
property.save(value)
}
static func save(_ data: [Property : Any]) {
data.forEach { property, value in
property.save(value)
}
}
static func get(_ property: Property) -> Any? {
return property.get()
}
}
@rlam3
After looking further into Expirable, it doesn't seem that will integrate into Cely. Now what feature of Expirable is it that you like? I like the idea of having expiring properties and allowing user's to handle what happens if a property is expired.
Would that solve the issue?
https://github.com/rlam3/moyajwtlogin
I think this is the best interpretation on how to might Expirable work with Cely. I had to create a separate SmartToken to detect if it was expirable but because These properties were accessing a Singleton/Keychain.
Not sure if there is a better way to optimize it. Some parts of the code can be a bit messy. Would love your feedback on this. Thanks! @initFabian
Hey @rlam3
I'm so sorry about not getting back on this issue. This issue has my full attention now.
Agree... bits of the code are a bit missy 😛, but overall, I understand what the example is. I guess right now we're at a crossroad as far as implementing an expiring feature. As of right now, I see that we have 2 options(but feel free to bounce around new ideas):
- Implement a JWT decoder into Cely, in order to retrieve the date(👎)
- Add some sort of
expireAt(:)
method into Cely's API.(👍)
Just to throw around some ideas...
Maybe upon successful login, we can have a Cely.expireSession(at: Date, withMarginInterval margin: TimeInterval)
. Therefore Cely can log the user out if that Date
has been reached/passed.
Next, we need to be able to tell the developers, "Hey, the session is about to expire, you should go refresh the token." So using the TimeInterval work you've done, we can communicate back to the user that the session is about to expire.
Before going any further, what are your thoughts? If you like the idea of adding an expiring date, I can draft up the docs for it, and we can go from there as far as deciding what the API should look like.
btw, inside of MoyaLoginViewController you could've just used Cely.changeStatus(to: .loggedIn)
to change to the MainStoryboard. And in fact, if it's not clear from the docs, please update the documentation so we can better communicate that functionality.