-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
37 lines (31 loc) · 1.14 KB
/
api.js
File metadata and controls
37 lines (31 loc) · 1.14 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
import { User } from "./userSchema.js";
import { comparePassword } from "./util.js";
export const userExists = async (req, res) => {
try {
const username = req.body.username;
const query = { username };
let user = await User.findOne(query);
let response;
response = { exists: !user ? false : true };
res.status(200).send(response);
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
};
export const correctPassword = async (req, res) => {
try {
const username = req.body.username;
const password = req.body.password;
const query = { username };
let user = await User.findOne(query);
if (!user) { console.log("does not exist."); return res.status(404).send({ error: "user not found." }); }
let status = await comparePassword(password, user.password.encrypted);
let response;
response = { correct: !status ? false : true };
res.status(200).send(response);
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
};