-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
44 lines (36 loc) · 1.18 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { AfterworkJS } from 'kuaralabs-afterworkjs';
import { authMiddleware } from './middlewares/authMiddleware';
const SECRET_KEY = 'your_secret_key'; // Replace with your actual secret key
const DB_URL = 'your_database_url'; // Replace with your actual database URL
const DB_NAME = 'your_database_name'; // Replace with your actual database name
const app = new AfterworkJS({
secret: SECRET_KEY,
dbType: "postgres", // Replace with the databases that is supported
dbConfig: {
dbUrl: DB_URL,
dbName: DB_NAME
}
});
const protectedRouteHandler = (req: any, res: any) => {
if (req.user) {
res.json({ message: 'Access granted', user: req.user });
} else {
res.statusCode = 401;
res.end('Unauthorized');
}
};
const routeWithMiddleware = (middlewares: any[], finalHandler: any) => {
return (req: any, res: any) => {
let i = 0;
const next = () => {
if (i < middlewares.length) {
middlewares[i++](req, res, next);
} else {
finalHandler(req, res);
}
};
next();
};
};
app.addRoute('get', '/protected', routeWithMiddleware([authMiddleware(SECRET_KEY)], protectedRouteHandler));
app.start(3000);