-
Notifications
You must be signed in to change notification settings - Fork 0
/
express_server.js
140 lines (114 loc) · 3.3 KB
/
express_server.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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const morgan = require("morgan");
const app = express();
const port = 4000;
const cors = require("cors");
app.use(cors());
app.use(morgan("tiny"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(passport.initialize());
const jwt = require("jsonwebtoken");
mongoose
.connect(
"mongodb+srv://Anbu:[email protected]/StartupSense",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => console.log("MongoDB connected"))
.catch((err) => console.log(err));
app.listen(port, () => console.log(`Server is running on port ${port}`));
const userSchema = new mongoose.Schema({
username: String,
email: String,
password: String,
bio: String
});
const User = mongoose.model('User', userSchema);
const createToken = (userId) => {
const expiresIn = 60 * 60 * 24 * 3;
const payload = { userId: userId };
const token = jwt.sign({userId}, "Q$r2K6W8n!jCW%Zk", { expiresIn });
return token;
};
app.post("/register", (req, res) => {
console.log(req.body);
const {
username,
email,
password,
confirm_password,
bio
} = req.body;
if (password !== confirm_password) {
res.status(400).json({ message: "Password incorrect" });
}
const newUser = new User({
username,
email,
password,
bio
});
console.log(newUser);
newUser
.save()
.then((user) => {
res.status(200).json({ message: "User registered successfully" });
})
.catch((err) => {
console.log("error in saving the user", err);
res.status(500).json({ message: err });
});
});
app.post("/login", (req, res) => {
const { username, password } = req.body;
User.findOne({ username })
.then((user) => {
if (!user) {
res.status(400).json({ message: "User not found" });
}
if (user.password !== password) {
res.status(400).json({ message: "Password incorrect" });
}
const token = createToken(user._id);
res.status(200).json({ token });
})
.catch((err) => {
console.log("error in finding the user", err);
res.status(400).json({ message: err });
});
});
app.get("/userprofile", async(req, res) => {
const authHeader = req.headers.authorization;
if(!authHeader){
console.log("Nothing");
return res.status(401).json({error: 'No token'});
}
const token = authHeader.split(" ")[1];
jwt.verify(token, "Q$r2K6W8n!jCW%Zk", async(err, decoded) => {
if(err){
console.log("Error: ", err);
return res.status(401).json({error: 'Invalid token'});
}
console.log("Decoded: ", decoded);
try{
const user = await User.findById(decoded.userId);
if(!user){
return res.status(404).json({error: 'Invalid user'});
}
res.status(200).json({
username: user.username,
email: user.email,
bio: user.bio
});
}catch (error) {
console.error("Error fetching user:", error);
return res.status(500).json({ error: 'Server error while retrieving user' });
}
} )
})