Handling Access Token Expiration
The JWT access token is only valid for a finite period of time.
Using an expired JWT will cause operations to fail. As you saw above, we are told how
long a token is valid through expires_in
. This value is normally 1200
seconds or 20 minutes. Expired tokens are not refreshed. We just fetch a new token and
pass it to the Live Experience SDK.
Timer.scheduledTimer(timeInterval: expires, target: self, selector: #selector(fetchToken), userInfo: nil, repeats: false)
Another is to compute an expiration time by adding 1200 seconds to the current system
time when the token was retrieved. So, if the time when you fetch the token was 10:00
the expiration time would be 10:20. An example of this in Swift looks like: let
expireTime = Date().addingTimeInterval(expires)
.
You then compare the current time to the expiration time to see if the token has expired.
UserDefaults.standard.set(token, forKey: "LXtoken")
UserDefaults.standard.set(expireTime, forKey: "tokenExpireTime")