-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
93 lines (78 loc) · 2.24 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
require("dotenv").config();
require("./config/database").connect();
const bcrypt = require("bcryptjs/dist/bcrypt");
const jwt = require("jsonwebtoken");
var path = require("path");
const express = require("express");
const app = express();
const User = require("./model/user");
const auth = require("./middleware/auth");
// Middlewares
app.use(express.json());
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.get("/", async (req, res) => {
res.render("index", {
baseURL: " https://demo-api-ex.herokuapp.com/api/v1/",
});
});
app.post("/api/v1/register", async (req, res) => {
// Register
try {
const { first_name, last_name, email, password } = req.body;
if (!(email && password && first_name && last_name)) {
res.status(400).json({
error: {
body: "All Fields Are Required",
},
});
}
const odlUser = await User.findOne({ email });
if (odlUser) {
return res.status(409).json({
error: {
body: "Already Registed with this email",
},
});
}
encryptedPassword = await bcrypt.hash(password, 10);
const user = await User.create({
first_name,
last_name,
email: email.toLowerCase(),
password: encryptedPassword,
});
var token = await user.signToken();
return res.status(201).json({ user: user.userJSON(token) });
} catch (err) {
console.error(err);
}
});
app.post("/api/v1/login", async (req, res) => {
// Login
try {
const { email, password } = req.body;
if (!(email && password)) {
res.status("400").json({
error: {
body: "All Field are required",
},
});
}
const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ error: "Email Not Found" });
}
if (user && (await bcrypt.compare(password, user.password))) {
let token = await user.signToken();
return res.status(200).json({ user: user.userJSON(token) });
}
res.status(400).json({ warning: "Invalid Credential" });
} catch (error) {
console.error(error);
}
});
app.get("/api/v1/verifyToken", auth.verifyToken, (req, res, next) => {
res.status(200).send("Token is Working");
});
module.exports = app;