-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
147 lines (133 loc) · 3.39 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
const express = require('express');
const birdCall = require('./lib/birdApi');
const db = require('./lib/psql');
require('dotenv').config();
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const bcrypt = require('bcrypt');
const path = require('path');
const PORT = process.env.PORT;
const app = express();
app.use((req, res, next) => {
console.log(`serving ${req.method} request for uri: ${req.url}`);
if (req.body) {
console.log(req.body);
}
next();
});
app.listen(PORT, () => {
console.log(`Listening at ${PORT}`);
});
app.use(express.static('public'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
secret: 'it\'s a secret man',
resave: false,
}));
app.post('/login', (req, res) => {
console.log(req.session)
db.getUser(req.body.username)
.then(data => {
if (data.length) {
let salt = data[0].salt;
let servPassHash = data[0].password;
let sentPassHash = bcrypt.hashSync(req.body.password, salt);
if (sentPassHash === servPassHash) {
req.session.regenerate(() => {
req.session.user = req.body.username;
res.writeHead(200);
res.end();
});
} else {
res.writeHead(401);
res.end();
}
} else {
res.writeHead(401);
res.end();
}
})
.catch(err => console.log(err));
});
app.post('/signup', (req, res) => {
db.getUser(req.body.username)
.then((data) => {
if (data.length === 0) {
const salt = bcrypt.genSaltSync(10);
const hashedPass = bcrypt.hashSync(req.body.password, salt);
db.createUser(req.body.username, hashedPass, salt)
.then(data => {
req.session.regenerate(() => {
req.session.user = req.body.username;
res.writeHead(200);
res.end();
});
});
} else {
res.writeHead(401);
res.write('user exists');
res.end();
}
})
.catch(err => console.log(err));
});
app.post('/logout', (req, res) => {
req.session.destroy(() => {
console.log('hit');
res.writeHead(200);
res.write('loggedout');
res.end();
});
});
app.post('/birds', (req, res) => {
let user = req.session.user;
console.log('this is user session', req.session);
db.getUser(user)
.then(data => {
console.log('this is data', data);
let id = data[0].id;
db.createBird(req.body.birdType, req.body.location, id)
.then(data => {
res.writeHead(200);
res.write('bird added!');
res.end();
});
});
});
app.post('/map', (req, res) => {
const latLng = req.body;
const obj = latLng;
const birdCatcher = (data) => {
res.writeHead(200, { contentType: 'application/json' });
res.write(data);
res.end();
};
birdCall.call(obj, birdCatcher);
});
// get users most recent birds logged in db
app.get('/birds', (req, res) => {
db.getBirdsInDb(20)
.then(data => {
res.writeHead(200);
res.write(JSON.stringify(data));
res.end();
});
});
app.get('/profile', (req, res) => {
db.getUser(req.session.user)
.then(data => {
let id = data[0].id;
db.getBirdsByUser(id)
.then(data => {
res.writeHead(200);
res.write(JSON.stringify(data));
res.end();
});
});
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, './public/index.html'));
});