diesel-core 1.0.2 → 1.0.3
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 +10 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -64,10 +64,10 @@ app.options()
|
|
|
64
64
|
### Diesel supports cors out of the box
|
|
65
65
|
|
|
66
66
|
``` javascript
|
|
67
|
-
app.cors
|
|
67
|
+
app.use(cors{
|
|
68
68
|
origin: ['http://localhost:5173','*'],
|
|
69
|
-
methods: 'GET,POST,PUT,DELETE',
|
|
70
|
-
allowedHeaders: 'Content-Type,Authorization'
|
|
69
|
+
methods: ['GET','POST','PUT','DELETE'],
|
|
70
|
+
allowedHeaders: ['Content-Type','Authorization']
|
|
71
71
|
})
|
|
72
72
|
```
|
|
73
73
|
|
|
@@ -87,7 +87,7 @@ import jwt from 'jsonwebtoken';
|
|
|
87
87
|
const app = new Diesel();
|
|
88
88
|
|
|
89
89
|
async function authJwt (ctx:ContextType, server?:Server): Promise<void | Response> {
|
|
90
|
-
const token = await ctx.
|
|
90
|
+
const token = await ctx.cookies?.accessToken // Retrieve the JWT token from cookies
|
|
91
91
|
if (!token) {
|
|
92
92
|
return ctx.json({ message: "Authentication token missing" },401);
|
|
93
93
|
}
|
|
@@ -95,10 +95,7 @@ async function authJwt (ctx:ContextType, server?:Server): Promise<void | Respons
|
|
|
95
95
|
// Verify the JWT token using a secret key
|
|
96
96
|
const user = jwt.verify(token, secret); // Replace with your JWT secret
|
|
97
97
|
// Set the user data in context
|
|
98
|
-
ctx.
|
|
99
|
-
|
|
100
|
-
// Proceed to the next middleware/route handler
|
|
101
|
-
return ctx.next();
|
|
98
|
+
ctx.set('user',user);
|
|
102
99
|
} catch (error) {
|
|
103
100
|
return ctx.json({ message: "Invalid token" },403);
|
|
104
101
|
}
|
|
@@ -119,7 +116,7 @@ app.get("/api/user/register", async (ctx:ContextType) => {
|
|
|
119
116
|
// Example protected route (requires auth)
|
|
120
117
|
app.get("/api/user/profile", async (ctx:ContextType) => {
|
|
121
118
|
// This route is protected, so the auth middleware will run before this handler
|
|
122
|
-
const user = ctx.
|
|
119
|
+
const user = ctx.get.user
|
|
123
120
|
return ctx.json({
|
|
124
121
|
msg: "You are authenticated!" ,
|
|
125
122
|
user
|
|
@@ -222,7 +219,7 @@ app.addHooks('onSend',async (ctx, result) => {
|
|
|
222
219
|
async function authJwt (ctx:ContextType, server?:Server): Promise<void | Response> {
|
|
223
220
|
|
|
224
221
|
try {
|
|
225
|
-
const token = ctx?.
|
|
222
|
+
const token = ctx.cookies?.accessToken
|
|
226
223
|
if (!token) {
|
|
227
224
|
return ctx.json({ message: "Authentication token missing" },401);
|
|
228
225
|
}
|
|
@@ -294,13 +291,13 @@ app.get("/redirect",(ctx:ContextType) => {
|
|
|
294
291
|
**You can use set ***Multiparams***** , ***like this***
|
|
295
292
|
|
|
296
293
|
```javascript
|
|
297
|
-
app.get("/product/:productId/:productName)
|
|
294
|
+
app.get("/product/:productId/:productName")
|
|
298
295
|
```
|
|
299
296
|
|
|
300
297
|
```javascript
|
|
301
298
|
app.get("/hello/:id/",(ctx:ContextType) => {
|
|
302
|
-
const id = ctx.
|
|
303
|
-
const query = ctx.
|
|
299
|
+
const id = ctx.params.id
|
|
300
|
+
const query = ctx.query // you can pass query name also , you wanna get
|
|
304
301
|
return ctx.json({ msg: "Hello", id });
|
|
305
302
|
})
|
|
306
303
|
```
|