-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (70 loc) · 2.1 KB
/
app.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
require("dotenv").config();
require("./config/database").connect();
const express = require("express");
const User = require("./model/user");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const auth = require("./middleware/auth");
const app = express();
const { TOKEN_KEY } = process.env;
app.use(express.json());
app.post("/register", async (req, res) => {
try {
//get input
const { first_name, last_name, email, password } = req.body;
//validate input
if (!(first_name && last_name && email && password)) {
return res.status(400).send("all input is required");
}
// if use exist -> validate in db
const oldUser = await User.findOne({ email: email.toLowerCase() });
if (oldUser) {
return res.status(409).send("User already exists. Please login");
}
// encrypt password
const encryptedPassword = await bcrypt.hash(password, 10);
// create user
const user = await User.create({
first_name,
last_name,
email: email.toLowerCase(),
password: encryptedPassword,
});
// create token
const token = jwt.sign({ user_id: user._id, email }, TOKEN_KEY, {
expiresIn: "2h",
});
// save user tokens
user.token = token;
res.status(201).json(user);
} catch (err) {
console.log(err);
}
});
app.get("/login", async (req, res) => {
try {
// get input
const { email, password } = req.body;
// validate input
if (!(password && email)) {
return res.status(400).send("all input are require");
}
// validation user in db
const user = await User.findOne({ email: email.toLowerCase() });
if (user && (await bcrypt.compare(password, user.password))) {
// create token
const token = jwt.sign({ user_id: user._id, email }, TOKEN_KEY, {
expiresIn: "2h",
});
user.token = token;
return res.status(200).json(user);
}
return res.status(400).send("invalid credential");
} catch (err) {
console.log(err);
}
});
app.post("/welcome", auth, (req, res) => {
res.status(200).send("welcome <3");
});
module.exports = app;