-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (87 loc) · 2.45 KB
/
Copy pathindex.js
File metadata and controls
100 lines (87 loc) · 2.45 KB
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
const express = require("express");
const app = express();
const { urlencoded } = require("express");
const cors = require("cors");
require("dotenv").config();
const mongoose = require("mongoose");
const userDB = require("./model.js");
const { fetch } = require("node-fetch");
let password = "";
const database = () => {
return mongoose.connect(process.env.MONGO_URI);
};
global.fetch = fetch;
// app.use(express.static("../GraphicalPasswordFrontEnd/build"))
app.use(express.json({ limit: "50mb" }));
app.use(urlencoded({ extended: false, limit: "50mb" }));
app.use(
cors({
origin: "*",
credentials: true,
})
);
app.get("/", (req, res) => {
res.send("welcoe to node server");
})
app.post("/signup", async (req, res) => {
const { theme, email, links, id } = req.body;
const allUser = await userDB.find();
if (allUser.find((user) => user.email === email)) {
res.status(401).send("EMAIL EXISTS");
} else {
let encryptedData = global.Buffer.from(theme).toString("base64");
for (let i = 0; i < id.length; i++) {
encryptedData += global.Buffer.from(id[i]).toString("base64");
}
allLink = links.map((link) => link.id);
const user = {
email: email,
allId: allLink,
password: encryptedData,
theme,
};
await userDB.create(user);
res.status(200).send("user added successfully");
}
});
app.post("/login", async (req, res) => {
const { email, theme } = req.body;
const user = await userDB.find({ email: email });
if (user.length === 0) {
res.status(404).send("NO USER FOUND");
}
else {
const userTheme = user[0].theme;
if (userTheme === theme) {
const Ids = user[0].allId;
password = user[0].password;
res.status(200).json({ Ids });
} else {
res.status(400).send("THEME INCORRECT");
}
}
});
app.post("/loginVerify", async (req, res) => {
const { id, theme } = req.body;
let encryptedData = global.Buffer.from(theme).toString("base64");
for (let i = 0; i < id.length; i++) {
encryptedData += global.Buffer.from(id[i]).toString("base64");
}
if (encryptedData === password) {
res.status(200).send("successfully logged in");
} else {
res.status(401).send("ERROR");
}
});
const port = process.env.PORT || 5000;
const connectDatabase = async () => {
try {
await database();
app.listen(port, () => {
console.log(`server listening to port ${port}`);
});
} catch (error) {
console.log(error);
}
};
connectDatabase();