-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookies.js
More file actions
60 lines (39 loc) · 1.22 KB
/
cookies.js
File metadata and controls
60 lines (39 loc) · 1.22 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
import session from 'express-session';
import express from 'express';
const app = express();
app.use(express.json());
app.use(session({
secret: 'your-secret-key',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
const db = [];
let id = 0;
app.post('/register',(req,res)=>{
try {
const {name,email,password} = req.body; // user se lega
const user = {id: ++id, name,email,password}; // objects
db.push(user);
res.status(201).send({message:"User registered successfully"});
} catch (error) {
res.status(500).send({message:"Internal Server Error" });
}
})
app.post("/login", (req, res) => {
try {
const { email, password } = req.body;
const user = db.find(x=>x.email === email && x.password === password);
if (!user) {
res.status(401).send({ message: "Invalid email or password" });
}
const token = user.id + "-" + new Date().getTime();
res.cookie("token", token, { httpOnly: true });
res.send({ message: "Login successful" });
} catch (error) {
res.status(500).send({ message: "Internal Server Error" });
}
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});