-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (139 loc) · 3.84 KB
/
Copy pathindex.js
File metadata and controls
155 lines (139 loc) · 3.84 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const express = require("express")
const App = express()
const PORT = process.env.PORT || 5000
const cors = require('cors')
const mongoose = require('mongoose')
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
const UserModel = require("./Models/UserModel.js")
const dburi = "mongodb+srv://kevkanae:crysis123@blogcluster.utcvb.mongodb.net/webx?retryWrites=true&w=majority"
App.use(express.json())
App.use(cors())
try {
mongoose.connect(dburi, { useNewUrlParser: true, useUnifiedTopology: true }).then(() => {
App.listen(PORT)
console.log("Connected to database")
})
console.log("MongoDB Connection: ✔");
} catch (err) {
console.log(err);
throw err;
}
// Test
App.get('/',(req,res)=>{
res.send('Yeah it works')
})
// LLogin
var add_minutes = function (dt, minutes) {
return new Date(dt.getTime() + minutes * 60000);
};
App.post('/login',(req,res)=>{
const { email, password } = req.body;
// Check if email exists
UserModel.findOne(
{
userEmail: email,
},
(err, docs) => {
if (err) console.log(err);
else {
if (docs) {
// Compare passwords
if (bcrypt.compare(password, docs["userPassword"])) {
const token = jwt.sign(
{ email: docs["userEmail"], id: docs["_id"] },
"kamiwajinseides",
{ expiresIn: "1h" }
);
let now = new Date();
// Proceed to login
res.send({
code: 200,
response: docs,
expiresIn: add_minutes(now, 60),
signedToken: token,
userResponse: "Proceed",
});
} else
res.send({
code: 401,
userResponse: "Wrong Password",
});
} else {
// Register first
res.send({
code: 401,
userResponse: "Unregistered",
});
}
}
}
);
})
// Register
App.post('/register',async(req,res)=>{
const { name, email, password } = req.body;
const hashPassword = await bcrypt.hash(password, 7);
UserModel.findOne({ userEmail: email }, (err, docs) => {
if (err) console.log(err);
else {
if (docs) {
// Account already exists
res.send({
code: 422,
userResponse: "Account Exists",
});
} else {
// Create account
new UserModel({
userName: name,
userEmail: email,
userPassword: hashPassword,
})
.save()
.then(() => {
UserModel.findOne(
{
userEmail: email,
},
(err, docs) => {
if (err) console.log(err);
else {
if (docs) {
const token = jwt.sign(
{ email: docs["userEmail"], id: docs["_id"] },
"kamiwajinseides",
{ expiresIn: "1h" }
);
let now = new Date();
// Proceed to login
res.send({
code: 200,
response: docs,
expiresIn: add_minutes(now, 60),
signedToken: token,
userResponse: "Proceed",
});
} else {
// Register first
res.send({
code: 401,
userResponse: "Unregistered",
});
}
}
}
);
})
.catch((err) => {
console.log(err);
res.send({
code: 400,
response: err,
userResponse: "Oops",
});
});
}
}
});
})