-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (109 loc) · 3.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require("dotenv").config();
require("./config/database").connect();
const express = require("express");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const cors = require("cors")
const User = require("./model/user");
const auth = require("./middleware/auth");
const authRefresh = require("./middleware/authRefresh");
const app = express();
app.use(express.json({ limit: "50mb" }));
app.use(cors());
app.post("/register", async (req, res) => {
try {
// Get user input
const { first_name, last_name, email, password } = req.body;
// Validate user input
if (!(email && password && first_name && last_name)) {
res.status(400).send("All input is required");
}
// check if user already exist
// Validate if user exist in our database
const oldUser = await User.findOne({ email });
if (oldUser) {
return res.status(409).send("User Already Exist. Please Login");
}
//Encrypt user password
encryptedPassword = await bcrypt.hash(password, 10);
// Create user in our database
const user = await User.create({
first_name,
last_name,
email: email.toLowerCase(), // sanitize: convert email to lowercase
password: encryptedPassword,
});
// Create token
const token = jwt.sign({ user_id: user._id, email }, process.env.TOKEN_KEY, {
expiresIn: "1h",
});
const refreshToken = jwt.sign({ user_id: user._id, email }, process.env.REFRESH_TOKEN_KEY, {
expiresIn: "30d",
});
// save user token
user.token = token;
user.refresh_token = refreshToken;
// return new user
res.status(201).json(user);
} catch (err) {
console.log(err);
}
});
app.post("/login", async (req, res) => {
try {
// Get user input
const { email, password } = req.body;
// Validate user input
if (!(email && password)) {
res.status(400).send("All input is required");
}
// Validate if user exist in our database
const user = await User.findOne({ email });
if (user && (await bcrypt.compare(password, user.password))) {
// Create token
const token = jwt.sign({ user_id: user._id, email }, process.env.TOKEN_KEY, {
expiresIn: "1h",
});
const refreshToken = jwt.sign({ user_id: user._id, email }, process.env.REFRESH_TOKEN_KEY, {
expiresIn: "30d",
});
// save user token
user.token = token;
user.refresh_token = refreshToken;
// user
res.status(200).json(user);
} else {
res.status(400).send("Invalid Credentials");
}
} catch (err) {
console.log(err);
}
});
app.get("/check", auth, (req, res) => {
res.status(200).send("Welcome 🙌 ");
});
app.post("/issue", authRefresh, async (req, res) => {
const { email } = req.body;
if (!email) {
res.status(400).send("All input is required");
}
// Validate if user exist in our database
const user = await User.findOne({ email });
const token = jwt.sign({ user_id: user._id, email }, process.env.TOKEN_KEY, {
expiresIn: "1h",
});
user.token = token;
res.status(200).json(user);
});
// This should be the last route else any after it won't work
app.use("*", (req, res) => {
res.status(404).json({
success: "false",
message: "Page not found",
error: {
statusCode: 404,
message: "You reached a route that is not defined on this server",
},
});
});
module.exports = app;