-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
201 lines (166 loc) · 5.96 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
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
const express = require('express');
const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const path = require('path');
const app = express();
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const saltRounds = 10;
// const {getHomePage} = require('./routes/index');
// const {addPlayerPage, addPlayer, deletePlayer, editPlayer, editPlayerPage} = require('./routes/player');
const port = 8083;
// create connection to database
// the mysql.createConnection function takes in a configuration object which contains host, user, password and the database name.
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'adsbehes_manabid'
});
// connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
// configure middleware
app.set('port', process.env.port || port); // set express to use this port
app.set('views', __dirname + '/views'); // set express to look in this folder to render our view
app.set('view engine', 'ejs'); // configure template engine
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,authorization');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // parse form data client
app.use(express.static(path.join(__dirname, 'public'))); // configure express to use public folder
app.use(fileUpload()); // configure fileupload
let loggedIn_token = ''
app.post('/login', (request, response) => {
let { mobile, password } = request.body;
db.query(`SELECT * FROM users WHERE mobile=${mobile}`, (err, res) => {
return new Promise((resolve, reject) => {
if (res && res.length) {
bcrypt.compare(password, res[0].password, function (_, result) {
if (result) {
loggedIn_token = jwt.sign({
exp: Math.floor(Date.now() / 1000) + (60 * 60),
data: { ...res[0] }
}, 'privatekey');
let {
name,
email,
amount,
mobile,
score,
avatar_path,
national_code,
last_name,
} = res[0];
let userObject = {
name,
email,
amount,
mobile,
score,
avatar_path,
national_code,
last_name,
token: loggedIn_token
}
resolve(response.send(userObject));
}
else {
reject(response.send('username or password is invalid'));
}
})
}
else {
reject(response.send('username or password is invalid'));
}
})
})
})
app.get('/me', (request, response) => {
if (request.headers['authorization']) {
jwt.verify(loggedIn_token, 'privatekey', (err, authorizedData) => {
if (authorizedData) {
let {
name,
email,
amount,
mobile,
score,
avatar_path,
national_code,
last_name,
} = authorizedData.data;
let userObject = {
name,
email,
amount,
mobile,
score,
avatar_path,
national_code,
last_name,
token: loggedIn_token
}
response.send({ ...userObject })
}
else {
response.send('not logged in')
}
})
}
else {
response.send('not logged in')
}
})
app.post('/signup', (request, response) => {
let { name, last_name, mobile, email, password } = request.body;
if (name && last_name && mobile && password) {
db.query(`SELECT * FROM users WHERE mobile=${mobile}`, (err, res) => {
if (!res.length) {
db.query(`INSERT INTO users(name,last_name,mobile,email,password) VALUES('','',${mobile},'','')`, (err, res) => {
if (res)
response.send(true)
else {
response.send('not signed up')
}
})
}
else {
response.send('this mobile number exists')
}
})
}
else {
response.send('required fields are not sent')
}
})
// routes for the app
/*
app.get('/', getHomePage);
app.get('/add', addPlayerPage);
app.get('/edit/:id', editPlayerPage);
app.get('/delete/:id', deletePlayer);
app.post('/add', addPlayer);
app.post('/edit/:id', editPlayer);
*/
// set the app to listen on the port
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});