Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to integrate expirable into CelyUser to manage different tokens? #27

Open
rlam3 opened this issue Nov 14, 2017 · 3 comments
Open

How to integrate expirable into CelyUser to manage different tokens? #27

rlam3 opened this issue Nov 14, 2017 · 3 comments

Comments

@rlam3
Copy link
Collaborator

rlam3 commented Nov 14, 2017

@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()
    }
}
@initFabian initFabian changed the title How to integrate exportable into CelyUser to manage different tokens? How to integrate expirable into CelyUser to manage different tokens? Dec 1, 2017
@initFabian
Copy link
Collaborator

@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?

@rlam3
Copy link
Collaborator Author

rlam3 commented Dec 1, 2017

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

@initFabian
Copy link
Collaborator

initFabian commented Mar 9, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants