• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Toilal / auth-toolbox / #184

pending completion
#184

push

Toilal
chore(release): add missing shelljs dependency for release

290 of 314 branches covered (92.36%)

Branch coverage included in aggregate %.

519 of 524 relevant lines covered (99.05%)

28.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/src/token-decoder/jwt-token-decoder.ts
1
import { Token, TokenDecoder } from '../auth-toolbox'
2
import { DefaultTokenDecoder } from './default-token-decoder'
8✔
3
import decode from 'jwt-decode'
8✔
4

5
/**
6
 * {@link TokenDecoder} based on jwt-decode package to be used with JWT {@link Token}.
7
 * It check expiration dates from both server response and JWT `exp` claim, and supports decoding
8
 * of the JWT token.
9
 *
10
 * It should be given to {@link Auth} constructor through {@link AuthOptions.accessTokenDecoder}.
11
 */
12
export class JwtTokenDecoder extends DefaultTokenDecoder implements TokenDecoder {
8✔
13
  /**
14
   * @param expiredOffset Offset in millisecond to consider the token as expired.
15
   *                      use Number.NaN to disable expiration from token decoding
16
   */
17
  constructor (expiredOffset: number = 0) {
15✔
18
    super(expiredOffset)
9✔
19
  }
20

21
  decode (token: Token): any {
8✔
22
    return decode(token.value)
8✔
23
  }
24

25
  isExpired (token: Token, offset?: number): boolean {
8✔
26
    if (super.isExpired(token)) {
7!
27
      return true
×
28
    }
29

30
    if (offset === undefined) {
7✔
31
      offset = this.offset
7✔
32
    }
33

34
    if (Number.isNaN(offset)) {
7✔
35
      return false
1✔
36
    }
37

38
    const now = Math.round((Date.now() - offset) / 1000)
6✔
39
    const decoded = this.decode(token)
6✔
40
    if ('exp' in decoded && now >= decoded.exp) {
6✔
41
      return true
4✔
42
    }
43

44
    return false
2✔
45
  }
46
}
8✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc