-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
247 lines (159 loc) · 5.04 KB
/
Copy pathserver.js
File metadata and controls
247 lines (159 loc) · 5.04 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require('dotenv').config()
const { v4: uuidv4 } = require('uuid');
const express = require('express')
const nodeMailer = require('nodemailer')
const cors = require('cors')
const bcrypt = require('bcrypt')
const mongoose = require('mongoose')
const expSchema = require('./src/Schemas/expSchema')
const userSchema = require('./src/Schemas/userSchema')
const passwordResetSchema = require('./src/Schemas/passwordResetSchema')
const app = express()
const PORT = process.env.PORT || 3000
app.use(cors())
app.use(express.json())
mongoose.connect('mongodb://localhost:27017/expTracker')
.then(() => {
console.log("Connected to mongodb")
})
.catch((e) => {
console.log(e)
})
app.post('/grab-expenses', async (req, res) => {
const { user } = req.body
try {
const getExpenses = await expSchema.find({ password: user.password })
res.json({ getExpenses: getExpenses })
} catch (e) {
console.log(e)
res.status(500)
}
})
app.post('/create-expense', async (req, res) => {
try {
const { desc, cost, password } = req.body
const createSchema = await new expSchema({ desc: desc, cost: cost, password: password })
await createSchema.save()
res.status(200).json({ createSchema: createSchema })
} catch (e) {
console.log(e)
res.status(400).send(e)
}
})
app.delete('/delete-expense', async (req, res) => {
try {
const { id } = req.body
const deleteTheSchema = await expSchema.deleteOne({ _id: id })
res.json(200).json("Deleted")
} catch (e) {
console.log(e)
res.status(400).send(e)
}
})
app.post('/sign-up', async (req, res) => {
const { email, password } = req.body
try {
const findUser = await userSchema.findOne({ email: email })
if (findUser === null) {
const hashedPassword = await bcrypt.hash(password, 10)
const createdUser = await new userSchema({
email: email,
password: hashedPassword
})
await createdUser.save()
res.json({ createdUser: createdUser })
} else {
res.json("User already exists!")
}
}
catch (e) {
console.log(e)
}
})
app.post('/sign-in', async (req, res) => {
const { email, password } = req.body
try {
const user = await userSchema.findOne({ email: email })
if (user !== null) {
const isThePasswordTrue = await bcrypt.compare(password, user.password)
if (isThePasswordTrue) {
res.json({ user: user })
console.log(user)
} else {
res.json("Wrong password")
}
} else {
res.json("User Not Found")
}
} catch (e) {
console.log(e)
}
})
app.post('/send-code-reset', async (req, res) => {
const { email, html } = req.body
const auth = {
service: "gmail",
auth: {
user: process.env.CURRENTGMAIL,
pass: process.env.USER_PASS
}
}
const mailOptions = {
from: process.env.CURRENTGMAIL,
to: email,
subject: "Reset password expense tracker",
html: html
}
try {
const transporter = await nodeMailer.createTransport(auth)
const _res = await transporter.sendMail(mailOptions)
res.json(_res)
} catch (e) {
console.log(e)
console.error("Email sending error:", e);
res.status(500).json({ error: "Failed to send email" });
}
})
app.post('/create-secret-token', async (req, res) => {
const { email } = req.body
try {
const tokenUsers = await passwordResetSchema.find()
console.log('Users', tokenUsers)
const tokenUser = await passwordResetSchema.findOne({ email: email })
console.log('user', tokenUser)
if (!tokenUser) {
const token = await uuidv4()
const tokenUser = await new passwordResetSchema({
email: email,
secretToken: token
})
await tokenUser.save()
res.json({ "token": token })
} else {
const token = await uuidv4()
tokenUser.secretToken = token
await tokenUser.save()
res.json({ "token": token })
}
} catch (e) {
console.log(e)
res.json({ "error": e })
}
})
app.put('/update-user', async (req, res) => {
const { token, password } = req.body
try {
const findTokenUser = await passwordResetSchema.findOne({ secretToken: token })
const email = findTokenUser.email
const findUser = await userSchema.findOne({ email: email })
const encryptedPassword = await bcrypt.hash(password, 10)
findUser.password = encryptedPassword
await findUser.save()
res.json({res: "Sucessfully updated", email: email})
} catch (e) {
console.log(e)
}
})
app.listen(PORT, () => {
console.log(`Listening on PORT ${PORT}`)
})