@navios/jwt 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,19 +27,18 @@ yarn add @navios/jwt
27
27
  ### Basic Setup
28
28
 
29
29
  ```typescript
30
- import { inject } from '@navios/core';
31
- import { provideJwtService } from '@navios/jwt';
32
-
30
+ import { inject } from '@navios/core'
31
+ import { provideJwtService } from '@navios/jwt'
33
32
 
34
33
  const MyJwtService = provideJwtService({
35
34
  secret: 'your-secret-key',
36
35
  signOptions: {
37
- expiresIn: '1h'
36
+ expiresIn: '1h',
38
37
  },
39
38
  })
40
39
  // Or with factory
41
40
  const JwtService = provideJwtService(async () => {
42
- const config = await inject(ConfigService);
41
+ const config = await inject(ConfigService)
43
42
  return config.jwt
44
43
  })
45
44
  ```
@@ -49,20 +48,20 @@ const JwtService = provideJwtService(async () => {
49
48
  ```typescript
50
49
  // Create a token
51
50
  import { Injectable, syncInject } from '@navios/core'
52
- import { JwtService } from '../service/jwt.service.mjs'
53
51
  //or to load without options
54
52
  import { JwtService } from '@navios/jwt'
55
53
 
54
+ import { JwtService } from '../service/jwt.service.mjs'
55
+
56
56
  @Injectable()
57
57
  class AuthService {
58
58
  jwtService = syncInject(JwtService)
59
-
59
+
60
60
  async generateToken(userId: number, role: string) {
61
- const token = await this.jwtService.signAsync({ userId, role });
62
- return token;
61
+ const token = await this.jwtService.signAsync({ userId, role })
62
+ return token
63
63
  }
64
64
  }
65
-
66
65
  ```
67
66
 
68
67
  ### Verifying Tokens
@@ -70,14 +69,14 @@ class AuthService {
70
69
  ```typescript
71
70
  try {
72
71
  // Verify and decode a token
73
- const payload = await jwtService.verify(token);
74
- console.log(payload); // { userId: 123, role: 'admin', iat: 1234567890, exp: 1234571490 }
72
+ const payload = await jwtService.verify(token)
73
+ console.log(payload) // { userId: 123, role: 'admin', iat: 1234567890, exp: 1234571490 }
75
74
  } catch (error) {
76
75
  // Handle verification errors
77
76
  if (error instanceof TokenExpiredError) {
78
- console.error('Token expired');
77
+ console.error('Token expired')
79
78
  } else if (error instanceof JsonWebTokenError) {
80
- console.error('Invalid token');
79
+ console.error('Invalid token')
81
80
  }
82
81
  }
83
82
  ```
@@ -90,22 +89,23 @@ Configuration options for the JWT service:
90
89
 
91
90
  ```typescript
92
91
  interface JwtServiceOptions {
93
- signOptions?: SignOptions;
94
- secret?: string;
95
- publicKey?: string | Buffer;
96
- privateKey?: Secret;
97
- verifyOptions?: VerifyOptions;
92
+ signOptions?: SignOptions
93
+ secret?: string
94
+ publicKey?: string | Buffer
95
+ privateKey?: Secret
96
+ verifyOptions?: VerifyOptions
98
97
  secretOrKeyProvider?: (
99
98
  requestType: RequestType,
100
99
  token?: string,
101
- options?: SignOptions | VerifyOptions
102
- ) => Secret | Promise<Secret>;
100
+ options?: SignOptions | VerifyOptions,
101
+ ) => Secret | Promise<Secret>
103
102
  }
104
103
  ```
105
104
 
106
105
  ### Error Handling
107
106
 
108
107
  The library exports error classes from the underlying `jsonwebtoken` library:
108
+
109
109
  - `TokenExpiredError`: Thrown when the token is expired
110
110
  - `NotBeforeError`: Thrown when the token is not yet valid
111
111
  - `JsonWebTokenError`: Base class for all JWT errors