Skip to content

Commit 3474c20

Browse files
committed
update routes and emailer
1 parent 11384d6 commit 3474c20

File tree

8 files changed

+58
-34
lines changed

8 files changed

+58
-34
lines changed

api/activate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const app = express.Router();
55

66
app.get('/activate/:code([a-zA-Z0-9]{64})', async (req, res) => {
77
await db.query(
8-
'update users set activated=$1, activation_code=$2, where activation_code=$3',
8+
'update users set activated=$1, activation_code=$2 where activation_code=$3',
99
[true, null, req.params.code]
1010
);
1111

12-
res.send(200);
12+
res.sendStatus(200);
1313
});
1414

1515
module.exports = app;

api/createAccount.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ const makeActivationCode = (username) => {
2929
};
3030

3131
app.post('/create', async (req, res) => {
32+
const requiredFields = ['username', 'password', 'email'];
33+
if (
34+
!requiredFields.every((rf) => Object.keys(req.body).find((k) => k === rf))
35+
) {
36+
return res.status(403).json({
37+
error: 'Missing required field(s)'
38+
});
39+
}
40+
3241
try {
3342
await checkAccountExists(req.body.username, req.body.email);
3443
const activationCode = makeActivationCode(req.body.username);

api/emailer.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ const nodemailer = require('nodemailer');
22
// const sendmailTransport = require('nodemailer-sendmail-transport');
33

44
const transporter = nodemailer.createTransport({
5-
host: 'mail.topkek.us',
6-
port: 465,
7-
secure: true,
5+
host: 'topkek.us',
6+
port: 587,
7+
secure: false,
88
auth: {
99
user: process.env.MAIL_USER,
1010
pass: process.env.MAIL_PASS
11+
},
12+
tls: {
13+
rejectUnauthorized: false
1114
}
1215
});
1316

api/isLoggedIn.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ const app = express.Router();
66

77
const cert = fs.readFileSync('./keys/cert.pem');
88

9-
app.all('/logged', (req, res) => {
10-
jwt.verify(
11-
req.header('Authorization').split('Bearer ')[1],
12-
cert,
13-
(err, decoded) => {
14-
if (err) {
15-
return res.sendStatus(401);
16-
}
17-
res.send(decoded);
9+
app.get('/logged', (req, res) => {
10+
if (typeof req.header('Authorization') === 'undefined') {
11+
return res.sendStatus(401);
12+
}
13+
const token = req.header('Authorization').split('Bearer ')[1];
14+
if (typeof token === 'undefined') {
15+
return res.sendStatus(401);
16+
}
17+
jwt.verify(token, cert, (err, decoded) => {
18+
if (err) {
19+
return res.sendStatus(401);
1820
}
19-
);
21+
res.send(decoded);
22+
});
2023
});
2124

2225
module.exports = app;

api/login.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@ const app = express.Router();
88
const privateKey = readFileSync('./keys/privkey.pem');
99

1010
app.post('/login', async (req, res) => {
11+
const requiredFields = ['username', 'password'];
12+
if (
13+
!requiredFields.every((rf) => Object.keys(req.body).find((k) => k === rf))
14+
) {
15+
return res.status(403).json({
16+
error: 'Missing required field(s)'
17+
});
18+
}
19+
1120
try {
1221
const result = await db.query(
13-
'select username, account_id, password_hash from users where username=$1',
22+
'select username, id, password_hash from users where username=$1',
1423
[req.body.username]
1524
);
1625

1726
if (bcrypt.compareSync(req.body.password, result.rows[0].password_hash)) {
1827
const token = jwt.sign(
1928
{
2029
username: req.body.username,
21-
id: result.rows[0].account_id
30+
id: result.rows[0].id
2231
},
2332
privateKey,
2433
{

api/unauthorized.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ const app = express.Router();
77
const cert = fs.readFileSync('./keys/cert.pem');
88

99
app.all('*', (req, res, next) => {
10-
jwt.verify(
11-
req.header('Authorization').split('Bearer ')[1],
12-
cert,
13-
(err, decoded) => {
14-
if (err) {
15-
return res.sendStatus(401);
16-
}
10+
if (typeof req.header('Authorization') === 'undefined') {
11+
return res.sendStatus(401);
12+
}
13+
const token = req.header('Authorization').split('Bearer ')[1];
14+
if (typeof token === 'undefined') {
15+
return res.sendStatus(401);
16+
}
17+
jwt.verify(token, cert, (err, decoded) => {
18+
if (err) {
19+
return res.sendStatus(401);
20+
}
1721

18-
req.decodedToken = decoded;
22+
req.decodedToken = decoded;
1923

20-
next();
21-
}
22-
);
24+
next();
25+
});
2326
});
2427

2528
module.exports = app;

app.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ app.listen(process.env.HTTP_PORT, '0.0.0.0');
3030

3131
// set headers for all routes
3232
app.all('/*', (req, res, next) => {
33-
res.header('Access-Control-Allow-Origin', 'http://topkek.us:3000');
33+
res.header('Access-Control-Allow-Origin', '*');
3434
res.header('Access-Control-Allow-Credentials', 'true');
3535
res.header('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT');
36-
res.header(
37-
'Access-Control-Allow-Headers',
38-
'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
39-
);
36+
res.header('Access-Control-Allow-Headers', '*');
4037

4138
next();
4239
});

db/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const config = {
1111

1212
const pool = new Pool(config);
1313

14-
pool.query('truncate users');
14+
// pool.query('truncate users');
1515

1616
module.exports.query = (text, values) => {
1717
return pool.query(text, values);

0 commit comments

Comments
 (0)