JWTDecode.Android icon indicating copy to clipboard operation
JWTDecode.Android copied to clipboard

expiresIn is not implemented

Open cj-marius-duna opened this issue 8 months ago • 2 comments

Checklist

  • [x] I have looked into the Readme and Examples, and have not found a suitable solution or answer.
  • [x] I have looked into the API documentation and have not found a suitable solution or answer.
  • [x] I have searched the issues and have not found a suitable solution or answer.
  • [x] I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • [x] I agree to the terms within the Auth0 Code of Conduct.

Describe the problem you'd like to have solved

The iOS version of this library has expiresIn for a token. It would be nice to have it in Android too.

Describe the ideal solution

Check if token expiresIn certain amount of time. Useful when the app willing to refresh the token before it's expiration time.

Alternatives and current workarounds

No response

Additional context

No response

cj-marius-duna avatar Jun 20 '25 06:06 cj-marius-duna

fun isTokenAboutToExpire(jwtToken: String): Boolean {
    val jwt = JWT(jwtToken)
    if (jwt.isExpired(0L)) return true

    val iat = jwt.issuedAt
    val exp = jwt.expiresAt

    // If either iat or exp is null, we can't reliably calculate token lifetime
    if (iat == null || exp == null) return true

    val lifetimeMillis = exp.time - iat.time
    if (lifetimeMillis <= 0) return true

    val thresholdMillis = (lifetimeMillis * 0.7).toLong()
    val thresholdTime = Date(iat.time + thresholdMillis)

    val now = Date()
    return now.after(thresholdTime)
}

cj-marius-duna avatar Jun 20 '25 06:06 cj-marius-duna

Hi @cj-marius-duna , We will look into this and plan to address this request

pmathew92 avatar Jun 20 '25 06:06 pmathew92