-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
151 lines (124 loc) · 3.77 KB
/
app.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// express server
require('dotenv').config();
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const data = require('./data');
const cors = require("cors");
// middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
mongoose.connect(process.env.MONGODB_URI).then(() => {
console.log("Connected to DB");
});
const userSchema = new mongoose.Schema({
name: String,
email: String,
password: String
});
const User = mongoose.model("User", userSchema);
const userAuth = (req, res, next) => {
const token = req.headers["x-access-token"];
if (!token) {
return res.status(400).send("Token not found");
}
try {
const decoded = jwt.verify(token, "secret");
req.user = decoded;
next();
} catch (error) {
return res.status(400).send("Invalid Token");
}
};
app.get("/", async (req, res) => {
res.send("Hello World");
});
app.post("/api/register", async (req, res) => {
try {
const { name, email, password } = req.body;
// validate
if (!name || !email || !password) {
return res.status(400).send("All fields are required");
}
// check if user already exists
const existingUser = await User.findOne({ email: email });
if (existingUser) {
return res.status(400).send("User already exists");
}
// create user
const newUser = new User({ name, email, password });
await newUser.save();
const token = jwt.sign({ id: newUser._id }, "secret");
res.status(200).json({ token });
} catch (error) {
res.status(500).send(error.message);
}
});
app.post("/api/login", async (req, res) => {
try {
const { email, password } = req.body;
// validate
if (!email || !password) {
return res.status(400).send("All fields are required");
}
// check if user exists
const user = await User.findOne({ email });
if (!user) {
return res.status(400).send("User does not exist");
}
// check password
if (user.password !== password) {
return res.status(400).send("Invalid Credentials");
}
// create token
const token = jwt.sign({ id: user._id }, "secret");
res.status(200).json({ token });
} catch (error) {
res.status(500).send(error.message);
}
});
app.get("/api/users", userAuth, async (req, res) => {
const userId = req.user.id;
const user = await User.findById(userId);
res.status(200).json(user);
});
app.put("/api/update-user", userAuth, async (req, res) => {
try {
const userId = req.user.id;
const { name, password } = req.body;
const user = await User.findById(userId);
if (!user) {
return res.status(400).send("User does not exist");
}
if (name) {
user.name = name;
}
if (password) {
user.password = password;
}
await user.save();
res.status(200).send("User updated");
} catch (error) {
res.status(401).send(error.message);
}
});
app.delete('/api/delete-user', userAuth, async (req, res) => {
try {
const userId = req.user.id;
await User.findByIdAndDelete(userId);
res.status(200).send("User deleted");
} catch (error) {
res.status(401).send(error.message);
}
});
app.get("/api/items", (req, res) => {
res.status(200).json(data);
});
app.get("/api/protected", userAuth, (req, res) => {
res.status(200).json({ message: "This is a protected route" });
});
app.listen(3001, () => {
console.log("Server is running on port 3000");
});