impulse-api 3.0.4 → 3.0.6
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/package.json +1 -2
- package/readme.md +86 -8
- package/src/api.js +2 -2
- package/src/server.js +4 -6
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"test": "nyc --reporter=lcov --reporter=text-summary mocha",
|
|
13
13
|
"test-server": "node ./test/integration/test-server.js"
|
|
14
14
|
},
|
|
15
|
-
"version": "3.0.
|
|
15
|
+
"version": "3.0.6",
|
|
16
16
|
"engines": {
|
|
17
17
|
"node": ">=22"
|
|
18
18
|
},
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
"jsonwebtoken": "8.5.1",
|
|
26
26
|
"lodash": "4.17.21",
|
|
27
27
|
"morgan": "1.10.0",
|
|
28
|
-
"multer": "1.4.5-lts.2",
|
|
29
28
|
"path-to-regexp": "0.1.12"
|
|
30
29
|
},
|
|
31
30
|
"devDependencies": {
|
package/readme.md
CHANGED
|
@@ -95,16 +95,94 @@ Impulse-Api comes with [JSON web token](https://www.npmjs.com/package/jsonwebtok
|
|
|
95
95
|
|
|
96
96
|
Additionally, any variables that are encoded in the token are accessible once the token has been decoded via `inputs.decoded`.
|
|
97
97
|
|
|
98
|
-
Token
|
|
98
|
+
#### Token Generation
|
|
99
|
+
|
|
100
|
+
Token generation is handled via the `Auth` class. You must create an `Auth` instance with your `secretKey`, then you can generate and verify tokens without passing the secret key on every call.
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
const Impulse = require('impulse-api');
|
|
104
|
+
const Auth = Impulse.Auth;
|
|
105
|
+
|
|
106
|
+
// Create an Auth instance with your secret key
|
|
107
|
+
const auth = new Auth('your-secret-key');
|
|
108
|
+
|
|
109
|
+
// Generate a token (no need to pass secretKey again)
|
|
110
|
+
const token = auth.generateToken({
|
|
111
|
+
userId: '123',
|
|
112
|
+
username: 'john.doe',
|
|
113
|
+
role: 'admin'
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Verify a token (no need to pass secretKey again)
|
|
117
|
+
const decoded = auth.verifyToken(token);
|
|
118
|
+
console.log(decoded.userId); // '123'
|
|
119
|
+
console.log(decoded.username); // 'john.doe'
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Important:** The `Auth` class requires a `secretKey` in the constructor. If you don't provide one, it will throw an error.
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
// This will throw an error
|
|
126
|
+
const auth = new Auth(); // Error: Auth instance must be initialized with secretKey
|
|
127
|
+
|
|
128
|
+
// This is correct
|
|
129
|
+
const auth = new Auth('your-secret-key');
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
When using the server with `secretKey` in the config, the server automatically creates an `Auth` instance internally. You can also create your own `Auth` instances for token generation in your application code (e.g., in login routes).
|
|
133
|
+
|
|
134
|
+
**Example: Login Route**
|
|
135
|
+
|
|
136
|
+
Here's a complete example of a login route that generates tokens:
|
|
99
137
|
|
|
100
138
|
```js
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
139
|
+
// routes/auth.js
|
|
140
|
+
const Impulse = require('impulse-api');
|
|
141
|
+
const Auth = Impulse.Auth;
|
|
142
|
+
|
|
143
|
+
// Create Auth instance with your secret key (same as server config)
|
|
144
|
+
const auth = new Auth(process.env.SECRET_KEY || 'your-secret-key');
|
|
145
|
+
|
|
146
|
+
exports.login = {
|
|
147
|
+
name: 'login',
|
|
148
|
+
description: 'User login endpoint',
|
|
149
|
+
method: 'post',
|
|
150
|
+
endpoint: '/api/login',
|
|
151
|
+
version: 'v1',
|
|
152
|
+
inputs: {
|
|
153
|
+
email: {
|
|
154
|
+
required: true,
|
|
155
|
+
validate: (val) => typeof val === 'string' && val.includes('@')
|
|
156
|
+
},
|
|
157
|
+
password: {
|
|
158
|
+
required: true
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
run: (services, inputs, next) => {
|
|
162
|
+
// Authenticate user (check database, etc.)
|
|
163
|
+
const user = services.userService.authenticate(inputs.email, inputs.password);
|
|
164
|
+
|
|
165
|
+
if (!user) {
|
|
166
|
+
return next(401, { error: 'Invalid credentials' });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Generate token with user data
|
|
170
|
+
const token = auth.generateToken({
|
|
171
|
+
userId: user.id,
|
|
172
|
+
email: user.email,
|
|
173
|
+
role: user.role
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
next(200, {
|
|
177
|
+
token,
|
|
178
|
+
user: {
|
|
179
|
+
id: user.id,
|
|
180
|
+
email: user.email,
|
|
181
|
+
role: user.role
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
};
|
|
108
186
|
```
|
|
109
187
|
|
|
110
188
|
#### Custom JWT Validation
|
package/src/api.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const Server = require('./server');
|
|
2
2
|
const Errors = require('./errors');
|
|
3
|
-
const
|
|
3
|
+
const Auth = require('./auth');
|
|
4
4
|
|
|
5
5
|
class Api {
|
|
6
6
|
constructor(config) {
|
|
@@ -10,6 +10,6 @@ class Api {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
Api.Errors = Errors;
|
|
13
|
-
Api.Auth =
|
|
13
|
+
Api.Auth = Auth;
|
|
14
14
|
|
|
15
15
|
module.exports = Api;
|
package/src/server.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const express = require('express');
|
|
3
|
-
const multer = require('multer');
|
|
4
3
|
const cors = require('cors');
|
|
5
4
|
const morgan = require('morgan');
|
|
6
5
|
const pathToRegexp = require('path-to-regexp');
|
|
@@ -475,11 +474,10 @@ class Server {
|
|
|
475
474
|
method = route.method.toLowerCase();
|
|
476
475
|
verb = (verbMap[method]) ? verbMap[method] : method;
|
|
477
476
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
}
|
|
477
|
+
// express-fileupload handles multipart/form-data (including files)
|
|
478
|
+
// express.json() handles JSON bodies
|
|
479
|
+
// multer().none() is not needed and conflicts with file uploads
|
|
480
|
+
this.http[verb](route.endpoint, this.preprocessor.bind(this, route));
|
|
483
481
|
|
|
484
482
|
});
|
|
485
483
|
});
|