-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (57 loc) · 1.69 KB
/
Copy pathserver.js
File metadata and controls
74 lines (57 loc) · 1.69 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
64
65
66
67
68
69
70
71
72
73
74
const express = require("express");
const connectToMongoDB = require("./connectMongodb");
const JobPost = require("./models/job");
const User = require("./models/user");
const jwt = require("jsonwebtoken")
const cookieParser = require("cookie-parser")
const cors = require("cors")
const app = express();
app.use(express.json())
app.use(cookieParser())
app.use(cors({
origin: ["http://localhost:5173"],
credentials:true
}))
app.post("/newjob", async (req,res) => {
const {title, description, role, contact} = req.body;
const result = await JobPost.create({title, description, role, contact});
res.json(result)
})
app.get("/getalljobs", async (req,res) => {
const result = await JobPost.find();
res.json(result)
})
app.post("/signup", async (req,res) => {
const {fullname, email, password} = req.body;
const user = await User.create({
fullname,
email,
password,
})
res.cookie("token",user.email,{
httpOnly: true,
secure: true,
sameSite: "None"
}).json(user)
})
app.post("/login", async (req,res) => {
const { email, password} = req.body;
const user = await User.findOne({email})
res.cookie("token",user.email,{
httpOnly: true, // Prevents JS from accessing it
secure: true, // Required for HTTPS
sameSite: "None"
}).json(user)
})
app.get("/logout", async (req,res) => {
res.clearCookie("token").json({success:true});
})
app.get("/user", async (req,res) => {
const token = req.cookies.token;
const user = await User.findOne({email: token})
res.json(user)
})
app.listen(8080, async () => {
await connectToMongoDB();
console.log("Server Started");
})