daneden / Twift

🐦 An async Swift library for the Twitter v2 API 🚧 WIP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Refresh Token Issue

emin-grbo opened this issue Β· comments

commented

Not sure if my implementation is not correct or if in the SPM itself, but still having the same refresh token issues πŸ€·β€β™‚οΈ
Posting here just in case as these days I am quite in a time-crunch so will surely forget πŸ˜…

Issue:
After access token has expired, it either does not refresh properly or new token is not propagated.

commented

@daneden
It seems token is not refreshed even though it has expired. I will investigate more, but i managed to make the app work by actually giving it a date that has definitely in the past
expiredOauthUser.expiresAt = Date(timeIntervalSinceNow: -10)

From there, token refreshes and works great πŸ™Œ

commented

I am just logging my train πŸš‚ of thought here, to come back to it later on if needed :)

Seems that decoding oauthUser will always "push back" the real expiry as it is using the current date. So whenever it is decoded from wherever it was saved, it will add seconds(7200) to the current date. That way, it never expires?
expiresAt = Date().addingTimeInterval(expiresIn) - Twift+Authentication.swift, line 291

commented

Made a PR for this issue:
#33

Hmm, this is a tricky one since we actually need to encode the date that the token was issued to avoid this problem. expiresIn by itself is useless since it doesn't have any reference date; encoding expiresAt might be the right thing to do here even though that property doesn't actually exist on the token issued from Twitter's server.

commented

I tried doing that in the PR but as you noted it had some issues and mixed history.
But idea was to init the Date() ONLY when doing the actual refresh/auth, not when doing the encoding/decoding. So, date is still encoded/decoded, but calculated only when initially gotten from the back.

Hopefully, PR will be clearer on the next try once I have proper history :)