-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
275 lines (234 loc) · 9.23 KB
/
routes.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var f = require('./functions.js')
const mongo = require('./mongoose.js')
const nodemailer = require('nodemailer')
const bcrypt = require('bcrypt')
const emailSenha = process.env.emailSenha
module.exports = function (app) {
app.get('/', (req, res) => {
res.render(__dirname + '/EJS/Home.ejs', {
login: '',
creatNow: false,
recarregar: false,
message: null,
games: false,
temporaly_games: null
})
})
app.get('/cadastro', (req, res) => {
res.render(__dirname + '/EJS/cadastro.ejs', {
message: ''
})
})
app.post('/cadastro_efetuado', async (req, res) => {
const email = req.body.email
const password1 = req.body.password1
const password2 = req.body.password2
const nome = req.body.nome
const confirmacao = f.gerarPassword()
if(password1 == '' || password2 == ''){
return f.alert('Coloque uma senha', res, '/cadastro.ejs')
} else if(password1 != password2){
return f.alert('As senhas não são iguais', res, '/cadastro.ejs')
}
if(nome.length > 25){
return f.alert('Nome grande demais, por favor, bote apenas um nome e um sobrenome', res, '/cadastro.ejs')
}
const exist = await mongo.seeIfCountExist(email)
if(exist == 'exist'){
return f.alert('Esse e-mail já possui uma conta', res, '/cadastro.ejs')
} else if(exist == "don't exist"){
} else {
return f.alert('Houve algum erro, tente novamente mais tarde', res, '/cadastro.ejs')
}
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: "[email protected]",
pass: emailSenha
}
})
const mailOptions = {
from: '[email protected]',
to: email,
subject: 'Pin da engine simples',
text: confirmacao
}
transporter.sendMail(mailOptions, function(err, info){
if (err) {
res.send(err)
} else {
res.render(__dirname + '/EJS/confirmar_email.ejs', {
message: '',
confirmacao: confirmacao,
email: email,
senha: password1,
nome: nome
})
}
})
})
app.post('/confirmar_email', async (req, res) => {
const codigo = req.body.codigo
const confirmacao = req.body.confirmacao
const userData = {
email: req.body.email,
password: req.body.senha,
username: req.body.username
}
if(codigo == confirmacao){
const exist = await mongo.seeIfCountExist(userData.email)
if(exist == 'exist'){
return res.render(__dirname + '/EJS' + '/confirmar_email.ejs', {
message: 'Esse email possui conta',
confirmacao: confirmacao,
email: userData.email,
senha: userData.password,
nome: userData.username
})
} else if(exist == "don't exist"){
} else {
return res.render(__dirname + '/EJS' + '/confirmar_email.ejs', {
message: 'Houve algum erro, tente novamente mais tarde',
confirmacao: confirmacao,
email: userData.email,
senha: userData.password,
nome: userData.username
})
}
const senhaCrypto = await bcrypt.hash(userData.password, 10)
mongo.saveConta(userData.email, senhaCrypto, userData.username)
res.render(__dirname + '/EJS/Home.ejs', {
creatNow: true,
login: userData.username,
senha: userData.password,
email: userData.email,
recarregar: true,
message: null,
games: false,
temporaly_games: null
})
} else {
return res.render(__dirname + '/EJS' + '/confirmar_email.ejs', {
message: 'O código não está correto',
confirmacao: confirmacao,
email: userData.email,
senha: userData.password,
nome: userData.username
})
}
})
app.get('/login', (req, res) => {
res.render(__dirname + '/EJS/login.ejs', {
message: ''
})
})
app.post('/login_efetuado', async (req, res) => {
const email = req.body.email
const password = req.body.password
const links = await mongo.links(email)
const titles = await mongo.titles(email)
const exist = await mongo.seeIfCountExist(email)
if(exist == 'exist'){
const conta = await mongo.findCount(email)
if(conta == 'error'){
return f.alert('Houve um erro, tente novamente mais tarde', res, '/login.ejs')
}
let compare = await bcrypt.compare(password, conta.senha)
if(!compare && (password != conta.senha || password.length > 15)){
return f.alert('A senha está errada', res, '/login.ejs')
}
res.render(__dirname + '/EJS/Home.ejs', {
login: conta.username,
creatNow: false,
recarregar: false,
message: null,
games: links.toString(),
titles: titles.toString(),
temporaly_games: null
})
} else if(exist == "don't exist"){
return f.alert('Não existe conta com esse email', res, '/login.ejs')
} else {
return f.alert('Houve um erro, tente novamente mais tarde', res, '/login.ejs')
}
})
app.post('/save_game', async (req, res) => {
const email = req.body.email
const senha = req.body.senha
const game = req.body.game
let username = ''
const exist = await mongo.seeIfCountExist(email)
if(exist == 'exist'){
const conta = await mongo.findCount(email)
let compare = await bcrypt.compare(senha, conta.senha)
if(conta == 'error'){
return f.alert('Houve um erro, tente novamente mais tarde', res, '/login.ejs')
} else if(!compare && (senha != conta.senha || senha.length > 15)){
return f.alert('A senha está errada', res, '/login.ejs')
} else {
username = conta.username
}
} else if(exist == "don't exist"){
return f.alert('Não existe conta com esse email', res, '/login.ejs')
} else {
return f.alert('Houve um erro, tente novamente mais tarde', res, '/login.ejs')
}
const link = await mongo.saveGame(game, email, username)
const url = '/game' + link
res.send(url)
})
app.get('/game/:link', async (req, res) => {
const link = req.params.link
const game = await mongo.getGame(link)
if(game == 'error'){
res.render(__dirname + '/EJS/Home.ejs', {
login: '',
creatNow: false,
recarregar: false,
message: game,
games: false,
temporaly_games: null
})
} else if(game == 'não encontrado'){
res.render(__dirname + '/EJS/Home.ejs', {
login: '',
creatNow: false,
recarregar: false,
message: game,
games: false,
temporaly_games: null
})
} else {
res.render(__dirname + '/EJS/game.ejs', {
game: JSON.stringify(game.game),
username: game.username,
titulo: game.game.titulo
})
}
})
app.get('/jogos', async (req, res) => {
res.render(__dirname + '/EJS/links.ejs', {})
})
app.post('/apagar', async (req, res) => {
await mongo.apagarLink(req.body.link)
res.redirect('jogos')
})
app.post('/editar', async (req, res) => {
var game = await mongo.getGame(req.body.link)
game.game.lnk = req.body.link
res.render(__dirname + '/EJS/Home.ejs', {
login: game.username,
creatNow: false,
recarregar: false,
message: null,
games: false,
temporaly_games: JSON.stringify(game.game)
})
})
app.post('/atualizar', async (req, res) => {
await mongo.atualizarJogo(req.body.link, req.body.game)
res.send(req.body.link)
})
}