-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (71 loc) · 2.6 KB
/
index.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
const express = require("express");
const cors = require("cors");
const connectDB = require("./db/mongooseConnection");
const User = require("./models/User");
const bcrypt = require('bcryptjs');
const { generateToken, authenticateToken } = require("./middleware/jwtAuth")
const app = express();
app.use(cors());
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// MongoDB
connectDB();
// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to Rest API." });
});
app.post("/api/register", async (req, res) => {
const { email, password } = req.body;
if (!email || !password) {
return res.status(400).json({ message: "Email or Password field required" });
}
const foundUser = await User.findOne({ email });
if (foundUser) {
return res.json({ message: "Email already in use!" });
}
try {
const hashPassword = await bcrypt.hash(password, 10)
const newUser = new User({ email, password: hashPassword });
await newUser.save();
res.status(201).json({ message: "Successfully registered" });
} catch (err) {
res.json({ message: "Failed to register something went wrong!", error: err.message });
}
});
app.post("/api/login", async (req, res) => {
try {
const { email, password } = req.body;
if (!email || !password) {
return res.status(400).json({ message: "Email or Password field required" });
}
const foundUser = await User.findOne({ email });
if (!foundUser) {
return res.status(401).json({ message: "Login Failed: Username or Password is Incorrect" });
}
const isPasswordMatched = await bcrypt.compare(password, foundUser.password);
if (isPasswordMatched) {
payload = {
_id: foundUser._id,
email: foundUser.email
}
token = generateToken(payload);
const { _id, email } = foundUser.toObject();
return res.status(200).json({ message: "Login Successfull!", _id, email, token: token });
} else {
return res.status(401).json({ message: "Login Failed: Username or Password is Incorrect" });
}
} catch (err) {
res.json({ message: "Login Error: Something went wrong", error: err.message });
}
})
app.get("/api/user", authenticateToken, async (req, res)=>{
// const foundUser = await User.findOne({_id: req.user._id});
res.status(200).send(req.user);
})
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});