-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.2 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
const express = require("express");
const app = express();
const port = 5000;
const mongoose = require("mongoose");
const bcryptjs = require("bcryptjs");
app.use(express.json());
const User = require("./User.js");
app.post("/register", async (req, res) => {
try {
const { email, password, name } = req.body;
const user = await User.findOne({ email });
if (user) {
return res.json({ message: "User already registered" });
} else {
// Hash the password;
let hashedPassword;
let salt = bcryptjs.genSaltSync(10);
hashedPassword = bcryptjs.hashSync(password, salt);
const newUser = new User({
email,
password: hashedPassword,
name
});
newUser.save();
return res.json({ message: "User created", user: newUser });
}
} catch (err) {
console.log(err);
}
});
mongoose
.connect(
"mongodb+srv://auth:[email protected]/auth?retryWrites=true&w=majority"
)
.then(() => console.log("Connected"))
.catch(err => console.log(err));
app.listen(port, () => console.log(`Server at ${port}`));