-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (83 loc) · 2.39 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
require("dotenv").config();
const express = require("express");
const { MongoClient } = require("mongodb");
const uri = process.env.MONGO_URI;
const yup = require("yup");
const bcrypt = require("bcrypt");
const cors = require('cors')
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors())
const saltRounds = 10;
const client = new MongoClient(uri);
let schema = yup.object().shape({
email: yup.string().email().required(),
password: yup.string().required(),
});
let collection;
const connectFunc = async () => {
try {
await client.connect();
collection = client.db().collection("user-details");
console.log("Connected successfully to server");
} catch {
console.dir;
}
};
app.post("/signup", async (req, res) => {
try {
try {
await schema.validate(req.body);
} catch (err) {
throw { statusCode: 421, message: err.message };
}
const response = await client
.db()
.collection("user-details")
.findOne({ email: req.body.email });
if (response) {
throw { statusCode: 409, message: "User already exists." };
}
const hash = bcrypt.hashSync(req.body.password, saltRounds);
await collection.insertOne({ email: req.body.email, password: hash });
res.status(201).send("Data insert successfully."); //add createdAt field
} catch (err) {
console.error(err);
res
.status(err.statusCode || 500)
.send(err.message || "Internal server error");
}
});
app.post("/login", async (req, res) => {
try {
try {
await schema.validate(req.body);
} catch (err) {
throw { statusCode: 421, message: err.message };
}
const result = await client
.db()
.collection("user-details")
.findOne({ email: req.body.email });
if(result == undefined) {
throw { statusCode: 404, message: "User does not exist." };
}
const response = bcrypt.compareSync(req.body.password, result.password);
if (response) {
res.send("Response validated!");
} else {
throw { statusCode: 401, message: "Invalid Password" };
}
} catch (err) {
console.error(err);
res
.status(err.statusCode || 500)
.send(err.message || "Internal server error");
}
});
connectFunc().then(() => {
app.listen(process.env.PORT || 3000, async () => {
console.log("Listening for requests...");
});
});