-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
263 lines (225 loc) · 6.81 KB
/
server.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
// If not in production require env variables
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
console.log("Secret:", process.env.SESSION_SECRET)
}
//Place holder user list till database setup
const users = [];
// Requirments
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const bcrypt = require('bcrypt');
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
// Establish the DB
let db = new sqlite3.Database('./db/example1.db', (err) => {
if (err) {
return console.error(err.message);
};
console.log('Connected to the in-memory SQlite database.');
});
const app = express();
// Set view engine to ejs
app.set('view engine', 'ejs')
app.engine('html', require('ejs').renderFile);
//Use Statements
app.use(express.json());
app.use(express.urlencoded({ extended: false}))
app.use(flash())
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
// Init passport with config file
const initializePassport = require('./passport-config');
// Call the initialize passport function from passport-config.js
async function getUser(email = null, id = null) {
let sql;
try {
email ? sql = `SELECT user_id, email, password FROM users WHERE email = "${email}"`
: sql = `SELECT user_id WHERE user_id = "${id}"`;
} catch {
return null;
}
return new Promise(function(res, rej) {
db.get(sql, [], (err, row) => {
if (err) {
return rej(err);
}
console.log(row)
db.close();
res(row);
});
});
};
initializePassport(passport,
email => getUser(email),
id => getUser(id));
app.post('/auth/google',
passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
// If new grant, insert it into the table, if editing a current grant, update that grant
app.post('/api/:grantid', (req, res) => {
const params = req.params
const data = req.body;
let sql;
params.grantid != 'null' ?
sql = `UPDATE grants SET grant = ?, status = ?, due_date = ?, amount_req = ?, amount_rec = ?, duration = ?, final_report = ?, interim_report = ? WHERE rowid = ${params.grantid}`
: sql = `INSERT INTO grants(grant, status, due_date, amount_req, amount_rec, duration, final_report, interim_report) VALUES(?, ? ,? ,? ,? ,?, ?, ?)`;
db.run(sql, data, function(err) {
if (err) {
return console.log(err.message);
}
console.log(`A row has been inserted with rowid ${this.lastID}`);
});
//db.close();
console.log('The database has been closed.');
res.status(200).send();
});
app.get('/', (req, res) => {
res.render('index');
});
// Render the dropdown option on the grant form
app.get('/options', (req, res) => {
sql = `SELECT * FROM status ORDER BY status`;
db.all(sql, {}, (err, data) => {
if (err) {
res.end();
console.log("Error!");
return;
} else {
res.json(data);
console.log(data);
};
});
});
app.get('/grants', (req, res) => {
res.render('table');
});
app.get('/grants/data/:sortBy', (req, res) => {
const sorter = req.params;
console.log(sorter.sortBy);
let sql;
if (sorter.sortBy != "all"){
sql = `SELECT rowid,
COALESCE(grant, "") AS grant,
COALESCE(status, "") AS status,
COALESCE(due_date, "") AS due_date,
COALESCE(amount_req, "") AS amount_requested,
COALESCE(amount_rec, "") AS amount_received,
COALESCE(duration, "") AS duration,
COALESCE(interim_report, "") AS interim_report,
COALESCE(final_report, "") AS final_report
FROM grants
WHERE status= "${sorter.sortBy}"`;
}
else {
sql = `SELECT rowid,
COALESCE(grant, "") AS grant,
COALESCE(status, "") AS status,
COALESCE(due_date, "") AS due_date,
COALESCE(amount_req, "") AS amount_requested,
COALESCE(amount_rec, "") AS amount_received,
COALESCE(duration, "") AS duration,
COALESCE(interim_report, "") AS interim_report,
COALESCE(final_report, "") AS final_report
FROM grants`;
};
console.log(sql)
db.all(sql, {}, (err, data) => {
if (err) {
res.end();
console.log("Error retrieving table data.");
return;
} else {
// res.json(data);
res.json(data);
console.log(data);
};
});
});
app.get(`/grant/:grantid`, (req, res) => {
const data = req.params;
const sql = `SELECT COALESCE(grant, "") AS grant,
COALESCE(status, "") AS status,
COALESCE(due_date, "") AS due_date,
COALESCE(amount_req, "") AS amount_requested,
COALESCE(amount_rec, "") AS amount_received,
COALESCE(duration, "") AS duration,
COALESCE(interim_report, "") AS interim_report,
COALESCE(final_report, "") AS final_report
FROM grants
WHERE rowid = ${data.grantid}`;
db.get(sql, (err, data) => {
if (err) {
res.end();
console.log("Error retrieving table data.");
return;
} else {
res.json(data);
console.log(data);
};
});
});
app.get(`/delete/:grantid`, (req, res) => {
const data = req.params
db.run(`DELETE FROM grants WHERE rowid = ${data.grantid}`, (err) => {
if (err) {
return console.log("Error deleting grant");
};
});
res.redirect('/grants');
});
app.get('/login', (req, res) => {
res.render('login.ejs');
});
app.get('/register', (req, res) => {
res.render("register.ejs");
});
// If login successful redirect to grant table, otherwise redirect back to login
app.post('/login', passport.authenticate('local', {
successRedirect: '/grants',
failureRedirect:'/login',
failureFlash: true
}))
app.post('/register', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const uniqueId = new Date()
let data = [uniqueId, req.body.email, hashedPassword]
console.log(data)
let sql = `INSERT INTO users(user_id, email, password) VALUES(?, ?, ?)`;
db.run(sql, data, (err) => {
if (err) {
return console.error(err.message);
}
});
res.redirect('/login')
} catch {
res.redirect('/register')
}
console.log(users)
});
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/grants');
});
app.get('/logout', function(req, res){
req.logout();
res.end()
});
app.listen(3000, () => console.log('listening at 3000'))
app.use("/public", express.static('public'));
app.use(express.json({ limit: '1mb' }));