-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
63 lines (44 loc) · 1.21 KB
/
Copy pathapp.js
File metadata and controls
63 lines (44 loc) · 1.21 KB
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
61
62
63
import express from "express";
import * as cyber from "./src/utils/cyber.js";
const app = express();
app.use(express.json());
let users = [];
app.post("/register", (req, res) => {
const { email, password } = req.body;
// Check if the user already exists - optional
// Create a new user that has id, email and password
// Generate a JWT that includes the user's info
res.status(201).json({
message: "User registered successfully",
token,
});
});
app.post("/login", (req, res) => {
const { email, password } = req.body;
// Find the user in our in-memory array
// Generate a JWT for the user
res.json({
message: "Logged in successfully",
token,
});
});
app.get("/protected", (req, res) => {
let token = req.headers.authorization;
if (!token) {
return res.status(401).json({ message: "Missing token" });
}
//Extract the actual token
try {
// Verify and decode the token
res.json({
message: "Protected route accessed",
decoded,
});
} catch (error) {
res.status(403).json({ message: "Invalid or expired token" });
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});