onasty/web/src/JwtUtil.elm (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com scaffold frontend app (#134)..., 11 months ago
ss2316544@gmail.com scaffold frontend app (#134)..., 11 months ago
| 1 | module JwtUtil exposing (isExpired) |
| 2 | |
| 3 | import Jwt |
| 4 | import Time |
| 5 | |
| 6 | |
| 7 | {-| Checks if a JWT token is expired or about to expire. |
| 8 | -} |
| 9 | isExpired : Time.Posix -> String -> Bool |
| 10 | isExpired now token = |
| 11 | let |
| 12 | expirationThreshold : number |
| 13 | expirationThreshold = |
| 14 | 40 * 1000 |
| 15 | |
| 16 | timeDiff : Int |
| 17 | timeDiff = |
| 18 | getTokenExpiration token |
| 19 | |> (\expiration -> expiration - Time.posixToMillis now) |
| 20 | in |
| 21 | timeDiff <= expirationThreshold |
| 22 | |
| 23 | |
| 24 | {-| Extracts the expiration time (in millis) from a JWT token. |
| 25 | Returns 0 if cannot parse token. |
| 26 | -} |
| 27 | getTokenExpiration : String -> Int |
| 28 | getTokenExpiration token = |
| 29 | Jwt.getTokenExpirationMillis token |
| 30 | |> Result.withDefault 0 |