0

I'm a beginner in swift. I've got json web token (jwt) and after extraction I have this payload:

exdate": 2016-07-13 10:55:27, "pack_id": 1, "sub": 10510, "pack_tag": R,

"ref": sxojAVrPADqrVrJ2JiS8j0JMmQIVSKNepaagwBte9vJH1pSDE2OBhUcia59u2q0tOXTcDSBtsZ1I40yMnkmdAg==,

"nbf": 1465891091, "exp": 1465892291, "iat": 1465891091 etc.

Now, My target is when the current time is greater than the exp I will refresh the token ref from the server. Now the question is: How can i check current time is greater than the exp?

2 Answers2

0
    let dateFormatter = NSDateFormatter()

    dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"

    let dateTimeStamp = NSDate(timeIntervalSince1970:1465891091)
    let now = NSDate()
    if( now.timeIntervalSinceDate(dateTimeStamp!) > 0 ) {
        print("now > dateTimeStamp")
    } else {
        print("Nope")
    }
tbilopavlovic
  • 1,096
  • 7
  • 13
0

Here it is updated for Swift 5 (JWT Formatting).

//Time in seconds since 1970.
let expiry = 1465891091

//if expirationDate > Now
if Date(timeIntervalSince1970: Double(expiry)) > Date() {
    print("JWT hasn't expired yet.")
} else {
    print("JWT has expired.")
}
Minimistro
  • 73
  • 7