-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (63 loc) · 1.86 KB
/
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
import express from 'express';
import mongoose from 'mongoose';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import cors from 'cors';
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
mongoose.connect(
'mongodb+srv://rizvan555:[email protected]/?retryWrites=true&w=majority',
{
dbName: 'vueDB',
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
const UserSchema = new mongoose.Schema({
username: { type: String, required: true },
email: { type: String, unique: true, required: true },
password: { type: String, required: true },
});
const User = mongoose.model('User', UserSchema);
// Register Route
app.post('/signUp', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const user = new User({
username: req.body.username,
email: req.body.email,
password: hashedPassword,
});
await user.save();
res.status(201).json({ message: 'User registered successfully' });
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Login Route
app.post('/signIn', async (req, res) => {
try {
const user = await User.findOne({ email: req.body.email });
if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}
const isPasswordValid = await bcrypt.compare(
req.body.password,
user.password
);
if (!isPasswordValid) {
return res.status(401).json({ error: 'Invalid credentials' });
}
const token = jwt.sign({ userId: user._id }, 'secretKey', {
expiresIn: '1h',
});
res.status(200).json({ token });
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});