-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
195 lines (162 loc) · 4.99 KB
/
app.js
File metadata and controls
195 lines (162 loc) · 4.99 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require("dotenv").config();
require("./config/database").connect();
const express = require("express");
const app = express();
app.use(express.json());
var bcrypt = require('bcryptjs');
var jwt = require('jsonwebtoken')
var auth = require("./middleware/auth");
// importing user context
const User = require("./model/user");
function listen(port) {
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
}
app.post("/welcome", auth, async (req, res) => {
res.status(200).send("Welcome 🙌 ");
});
// View Info
app.get("/myinfo", auth, async (req, res) => {
// get email from jwt token
const email = req.user.email;
// find the user by email
const userInfo = await User.findOne({email: email}, '-_id -__v -password');
// return user's information
res.status(200).json(userInfo);
});
app.patch("/update", auth, async (req, res) => {
//get user ID from jwt token
const email = req.user.email;
try {
// Find the user by ID
const user = await User.findOne({email: email});
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
// Update only the specified user details with the new values from the request body
if (req.body.first_name) {
user.first_name = req.body.first_name;
}
if (req.body.last_name) {
user.last_name = req.body.last_name;
}
if (req.body.email) {
user.email = req.body.email;
}
if (req.body.user_role) {
return res.status(409).send('You are not allowed to change your role.');
}
if (req.body.designation) {
user.designation = req.body.designation;
}
if (req.body.company) {
user.company = req.body.company;
}
if (req.body.password) {
//Encrypt user password
encryptedPassword = await bcrypt.hash(req.body.password, 10);
user.password = encryptedPassword;
}
// Save the updated user details to the database
await user.save();
// Return the updated user details
res.send("Successfully updated details");
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Server error' });
}
})
// Register
app.post("/register", async (req, res) => {
// Our register logic starts here
try {
// Get user input
const { first_name, last_name, email, password, user_role, designation, company } = req.body;
// Validate user input
if (!(email && password && first_name && last_name && user_role)) {
res.status(400).send("These inputs: first_name, last_name, email, password, user_role are required to register");
}
// check if user already exist
// Validate if user exist in our database
const oldUser = await User.findOne({ email: email });
if (oldUser) {
return res.status(409).send("User Already Exist. Please Login");
}
// check if role is valid
if (!(user_role == 'ADMIN' || user_role == 'TECHNICIAN' || user_role == 'MEMBER')) {
return res.status(409).send("user_role is not valid, valid roles include [ADMIN, MEMBER, TECHNICIAN].");
}
//Encrypt user password
encryptedPassword = await bcrypt.hash(password, 10);
// Create user in our database
const user = await User.create({
first_name,
last_name,
email: email.toLowerCase(), // sanitize: convert email to lowercase
password: encryptedPassword,
user_role: user_role,
designation: designation,
company: company,
});
// Create token
const token = jwt.sign(
{ user_id: user._id, email },
process.env.TOKEN_KEY,
{
expiresIn: "2h",
}
);
// save user token
await(user.token = token);
// return json web token
res.status(201).json(token);
} catch (err) {
console.log(err);
}
});
// Login
app.post("/login", async (req, res) => {
try {
// Get user input
const { email, password } = req.body;
// Validate user input
if (!(email && password)) {
res.status(400).send("All inputs(email and password) are required");
}
// Validate if user exist in our database
const user = await User.findOne({ email });
if (user && (await bcrypt.compare(password, user.password))) {
// Create token
const token = jwt.sign(
{ user_id: user._id, email },
process.env.TOKEN_KEY,
{
expiresIn: "2h",
}
);
// save user token
user.token = token;
// return json web token after logging in
res.status(200).json(token);
} else {
res.status(400).send("Invalid Credentials");
}
} catch (err) {
console.log(err);
}
});
app.delete('/delete', auth, async (req, res) => {
// Get the user ID from the JWT token
const email = req.user.email;
try {
// Find the user by ID and delete them
await User.findOneAndDelete({email: email});
// Return a success message to the client
res.json({ message: 'User deleted successfully' });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Server error' });
}
})
module.exports = app;