-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
335 lines (224 loc) · 7.87 KB
/
index.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const express = require('express');
const mysql = require('mysql');
const dotenv = require('dotenv');
const path = require('path');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const cookieParser = require('cookie-parser');
const expressSession = require('express-session');
const authMiddleware = require('./middleware/authMiddleware');
const { set } = require('express/lib/application');
const app = express();
const saltRounds = 10;
dotenv.config({ path: './.env' });
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: false }));
app.set('views', path.join(__dirname, 'views'));
app.set('components', path.join(__dirname, 'partials'));
const db = mysql.createConnection({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_ROOT,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE
})
// app.use(expressSession({
// secret: 'secret',
// resave: true,
// saveUninitialized: true
// }))
app.use(express.urlencoded({ extended: true, limit: '10kb' }));
app.use(cookieParser());
app.use('/',authMiddleware);
app.get('/', authMiddleware, (req, res) => {
res.render('index');
});
app.get('/login', (req, res) => {
res.render('login');
});
app.get('/register', (req, res) => {
res.render('register');
});
app.get('/logout',(req,res) =>{
res.clearCookie('token');
res.clearCookie('id');
res.redirect('/login');
});
app.get('/new_link',(req,res)=>{
//const new_link_user_id = req.cookies.id;
res.render('new_link');
});
app.get('/file_grups', (req, res) => {
let file = req.query.file;
let user_id = req.cookies.id;
const queryGroup = 'SELECT * FROM links WHERE file_grup = ? AND user_id = ?';
db.query(queryGroup, [file, user_id], (errorGroup, resultGroup) => {
if (errorGroup) {
console.log(errorGroup);
} else {
if (resultGroup.length > 0) {
// İkinci sorgu: Tüm "links" verilerini getirir
const queryAllLinks = 'SELECT * FROM links WHERE user_id = ?';
db.query(queryAllLinks, [user_id], (errorAllLinks, resultAllLinks) => {
if (errorAllLinks) {
console.log(errorAllLinks);
} else {
if (resultAllLinks.length > 0) {
const group = resultGroup;
const links = resultAllLinks;
console.log(group, links);
res.render('file_grups', { group: group, links: links });
}
}
});
}
}
});
});
app.get('/dashboard', (req, res) => {
const userEmail = req.email;
const userQuery = 'SELECT * FROM users WHERE email = ?';
db.query(userQuery, [userEmail], (userError, userResult) => {
if (userError) {
console.error(userError);
return res.status(500).send('Internal Server Error');
}
if (userResult.length === 0) {
res.clearCookie('token');
return res.redirect('/login');
}
// Kullanıcının bağlantılarını veritabanından alın
const userId = userResult[0].id;
const linksQuery = 'SELECT * FROM links WHERE user_id = ?';
db.query(linksQuery, [userId], (linksError, linksResult) => {
if (linksError) {
console.error(linksError);
return res.status(500).send('Internal Server Error');
}
const userData = userResult[0];
const linkData = linksResult;
res.render('dashboard' , { user: userData, link: linkData });
});
});
});
app.post('/login', (req, res) => {
const email = req.body.email;
const password = req.body.pass;
const query = 'SELECT * FROM users WHERE email = ? ';
const plainPassword = password;
db.query(query, [email], (error, result) => {
if (error) {
console.log(error);
} else {
if (result.length > 0) {
const userId = result[0].id;
const userName = result[0].name;
bcrypt.compare(plainPassword, result[0].password, (err, result) => {
if (err) {
console.error(err);
} else {
if (result) {
var token = jwt.sign({ email: email, date: new Date() }, process.env.JWT_SECRET)
res.cookie('token', token);
res.cookie('id', userId)
res.redirect('/dashboard');
} else {
res.status(401).send('Kullanıcı bilgileri yanlış');
}
}
});
} else {
res.status(401).send('Kullanıcı bilgileri yanlış');
}
}
});
});
app.post('/register', (req, res) => {
const name = req.body.name;
const email = req.body.email;
const password = req.body.pass;
const plainPassword = password;
bcrypt.hash(plainPassword, saltRounds, (err, hash) => {
if (err) {
console.error(err);
} else {
const query = 'INSERT INTO users SET name = ?, email = ?, password = ?';
db.query(query, [name, email, hash], (error, result) => {
if (error) {
console.log(error);
} else {
res.redirect('/login');
}
});
}
});
});
app.post('/new_link',(req,res)=>{
const linkname = req.body.linkname;
const file_grup = req.body.file_grup;
const link = req.body.link;
const description = req.body.description;
const user_id = req.cookies.id;
const query = 'INSERT INTO links SET linkname = ?, file_grup = ?, link = ?, description = ? , user_id = ?';
db.query(query, [linkname, file_grup, link, description,user_id], (error, result) => {
if (error) {
console.log(error);
} else {
if(result){
res.redirect('/dashboard');
}
}
});
});
app.get('/link_delete', (req, res) => {
const id = req.query.id;
console.log(id);
const query = 'DELETE FROM links WHERE id = ?';
db.query(query, [id], (error, result) => {
if (error) {
console.log(error);
}else {
res.redirect('/dashboard');
}
});
});
app.get('/link_update/:id', (req, res) => {
const id = req.params.id;
const query = 'SELECT * FROM links WHERE id = ?';
db.query(query, [id], (error, result) => {
if (error) {
console.log(error);
res.status(500).send('Internal Server Error');
} else {
if (result.length > 0) {
const data = {
link: result[0]
};
res.render('link_update', data);
} else {
res.status(404).send('Link not found');
}
}
});
});
app.post('/link_update/:id', (req, res) => {
const id = req.params.id;
const { linkname, file_grup, link, description } = req.body;
const query = 'UPDATE links SET linkname = ?, file_grup = ?, link = ?, description = ? WHERE id = ?';
db.query(query, [linkname, file_grup, link, description, id], (error, result) => {
if (error) {
console.log(error);
res.status(500).send('Internal Server Error');
} else {
res.redirect('/dashboard');
}
});
});
app.listen(5000, () => {
db.connect((error) => {
if (error) {
console.log(error)
} else {
console.log(" Server started on port 5000 And Mysql Connected..")
}
});
});