-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (45 loc) · 1.67 KB
/
index.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express');
const jwt = require('jsonwebtoken');
const app = express();
const port = 3000;
// Middleware to parse JSON requests
app.use(express.json());
// Secret key for signing the JWT
const secretKey = 'lkp-key';
// API to generate JWT
app.post('/generate-token', (req, res) => {
const payload = req.body;
let token = null;
let url = null;
// Check if the payload is empty
if (Object.keys(payload).length > 0) {
// Generate JWT token
token = jwt.sign(payload, secretKey, { expiresIn: '30d' });
url = 'https://livekeeping-web.web.app/?JWT=' + token + '&isDebug=true';
// Send the token as a response with a 200 status code
res.status(200).json({ token: token, url: url });
} else {
// If payload is empty, send a 400 Bad Request status code
res.status(400).json({ error: 'Invalid payload. JSON body is required.' });
}
});
app.post('/decode-token', (req, res) => {
const { token } = req.body;
if (!token) {
// If no token is provided, send a 400 Bad Request status code
res.status(400).json({ error: 'Token is required.' });
return;
}
try {
// Verify and decode the token
const decoded = jwt.verify(token, secretKey);
// Send the decoded data as a response with a 200 status code
res.status(200).json({ data: decoded });
} catch (error) {
// If the token is invalid or expired, send a 401 Unauthorized status code
res.status(401).json({ message: 'Invalid or expired token.' , error: error});
}
});
app.listen(port, () => {
console.log(`JWT API is running on http://localhost:${port}`);
});