Skip to content

Commit 11384d6

Browse files
committed
modernize codebase
1 parent 801baef commit 11384d6

19 files changed

+3567
-1914
lines changed

.eslintrc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
node: true
5+
},
6+
extends: ["airbnb-base"],
7+
plugins: ["prettier"],
8+
rules: {
9+
"quotes": [2, "single", { "avoidEscape": true }],
10+
"no-console": process.env.NODE_ENV === "production" ? "error" : "off",
11+
"no-debugger": process.env.NODE_ENV === "production" ? "error" : "off",
12+
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
13+
"comma-dangle": ["error", "never"],
14+
"prettier/prettier": "error",
15+
"implicit-arrow-linebreak": "off",
16+
"arrow-parens": "off",
17+
"arrow-body-style": "off",
18+
"no-param-reassign": "off",
19+
"no-plusplus": "off",
20+
"consistent-return": "off"
21+
},
22+
parserOptions: {
23+
parser: "babel-eslint"
24+
}
25+
};

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"arrowParens": "always",
4+
"printWidth": 80
5+
}

api/activate.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const db = require('./../db.js');
2-
const express = require('express');
3-
4-
const app = express.Router();
5-
6-
app.get('/activate/:code([a-zA-Z0-9]{64})', (req, res) => {
7-
db.query('update tracker.user set activated=$1, activation_code=$2, where activation_code=$3',
8-
[true, null, req.params.code]).then(result => {
9-
res.send(200);
10-
});
11-
});
12-
13-
module.exports = app;
1+
const express = require('express');
2+
const db = require('./../db');
3+
4+
const app = express.Router();
5+
6+
app.get('/activate/:code([a-zA-Z0-9]{64})', async (req, res) => {
7+
await db.query(
8+
'update users set activated=$1, activation_code=$2, where activation_code=$3',
9+
[true, null, req.params.code]
10+
);
11+
12+
res.send(200);
13+
});
14+
15+
module.exports = app;

api/blogPosts.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
const express = require('express');
2-
const db = require('./../db.js');
3-
4-
const app = express.Router();
5-
6-
app.get('/blogPosts/:amount', (req, res) => {
7-
db.query('select * from tracker.blogposts limit $1', [req.params.amount]).then(result => {
8-
res.json(result.rows.map((post, index) => {
9-
return { title: post.title, content: post.content, timestamp: post.time };
10-
}));
11-
});
12-
});
13-
14-
module.exports = app;
1+
const express = require('express');
2+
const db = require('./../db');
3+
4+
const app = express.Router();
5+
6+
app.get('/blogPosts/:amount', async (req, res) => {
7+
const result = await db.query('select * from blogposts limit $1', [
8+
req.params.amount
9+
]);
10+
11+
return res.json(
12+
result.rows.map((post) => {
13+
return {
14+
title: post.title,
15+
content: post.content,
16+
timestamp: post.time
17+
};
18+
})
19+
);
20+
});
21+
22+
module.exports = app;

api/createAccount.js

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,60 @@
1-
const express = require('express');
2-
const db = require('./../db.js');
3-
const bcrypt = require('bcrypt');
4-
const emailer = require('./emailer');
5-
const crypto = require('crypto');
6-
7-
const app = express.Router();
8-
9-
const checkAccountExists = (username, email) => {
10-
return db.query('select username from tracker.user where username=$1 or email=$2', [username, email]).then(result => {
11-
if (result.rows.length > 0) {
12-
return Promise.reject(new Error('Invalid username/email.'));
13-
}
14-
return Promise.resolve();
15-
});
16-
};
17-
18-
const hashPassword = password => {
19-
return bcrypt.hashSync(password, 10);
20-
};
21-
22-
const makeActivationCode = username => {
23-
const hash = crypto.createHash('sha256');
24-
const code = process.env.COOKIE_SECRET + username + Math.floor((new Date).getTime()/1000).toString();
25-
hash.update(code);
26-
return hash.digest('hex');
27-
};
28-
29-
app.post('/create', (req, res) => {
30-
checkAccountExists(req.body.username, req.body.email).then(() => {
31-
const activationCode = makeActivationCode(req.body.username);
32-
db.query('insert into tracker.user (username, password_hash, email, activation_code) values ($1, $2, $3, $4)',
33-
[req.body.username, hashPassword(req.body.password), req.body.email, activationCode]).then(result => {
34-
res.sendStatus(200);
35-
emailer.sendActivationEmail(req.body.email, activationCode);
36-
}).catch(err => {
37-
console.log(err);
38-
res.sendStatus(503);
39-
});
40-
}).catch(err => {
41-
console.log(err);
42-
res.status(409);
43-
res.send('Invalid username/email.');
44-
});
45-
});
46-
47-
module.exports = app;
1+
const express = require('express');
2+
const bcrypt = require('bcrypt');
3+
const crypto = require('crypto');
4+
const { ulid } = require('ulid');
5+
const db = require('./../db');
6+
const emailer = require('./emailer');
7+
8+
const app = express.Router();
9+
10+
const checkAccountExists = async (username, email) => {
11+
const result = await db.query(
12+
'select username from users where username=$1 or email=$2',
13+
[username, email]
14+
);
15+
if (result.rows.length > 0) {
16+
throw new Error('Invalid username/email.');
17+
}
18+
};
19+
20+
const hashPassword = (password) => bcrypt.hashSync(password, 10);
21+
22+
const makeActivationCode = (username) => {
23+
const hash = crypto.createHash('sha256');
24+
const code = `${process.env.SECRET}${username}${Math.floor(
25+
new Date().getTime() / 1000
26+
).toString()}`;
27+
hash.update(code);
28+
return hash.digest('hex');
29+
};
30+
31+
app.post('/create', async (req, res) => {
32+
try {
33+
await checkAccountExists(req.body.username, req.body.email);
34+
const activationCode = makeActivationCode(req.body.username);
35+
try {
36+
await db.query(
37+
'insert into users (id, username, password_hash, email, activation_code, activated) values ($1, $2, $3, $4, $5, $6)',
38+
[
39+
ulid(),
40+
req.body.username,
41+
hashPassword(req.body.password),
42+
req.body.email,
43+
activationCode,
44+
false
45+
]
46+
);
47+
res.sendStatus(200);
48+
emailer.sendActivationEmail(req.body.email, activationCode);
49+
} catch (err) {
50+
console.log(err);
51+
res.sendStatus(503);
52+
}
53+
} catch (err) {
54+
console.log(err);
55+
res.status(409);
56+
res.send('Invalid username/email.');
57+
}
58+
});
59+
60+
module.exports = app;

api/emailer.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
const nodemailer = require('nodemailer');
2-
//const sendmailTransport = require('nodemailer-sendmail-transport');
3-
4-
const transporter = nodemailer.createTransport({
5-
host: 'topkek.us',
6-
port: 25,
7-
secure: false,
8-
auth: {
9-
user: process.env.MAIL_USER,
10-
pass: process.env.MAIL_PASS
11-
}
12-
});
13-
14-
const makeActivationURL = (activationCode) => {
15-
return `http://${process.env.DOMAIN}:${process.env.WEB_PORT}/activate/${activationCode}`;
16-
};
17-
18-
module.exports.sendActivationEmail = (to, activationCode) => {
19-
const mailOptions = {
20-
from: 'Topkek <[email protected]>',
21-
to: to,
22-
subject: 'Email verification',
23-
text: `Please click on the link below to activate your account:\n\n ${makeActivationURL(activationCode)}`
24-
};
25-
26-
transporter.sendMail(mailOptions, (err, info) => {
27-
if (err) {
28-
console.log(err);
29-
}
30-
console.log(`Message sent: ${info.response}`);
31-
});
32-
};
1+
const nodemailer = require('nodemailer');
2+
// const sendmailTransport = require('nodemailer-sendmail-transport');
3+
4+
const transporter = nodemailer.createTransport({
5+
host: 'mail.topkek.us',
6+
port: 465,
7+
secure: true,
8+
auth: {
9+
user: process.env.MAIL_USER,
10+
pass: process.env.MAIL_PASS
11+
}
12+
});
13+
14+
const makeActivationURL = (activationCode) => {
15+
return `http://${process.env.DOMAIN}:${
16+
process.env.HTTP_PORT
17+
}/activate/${activationCode}`;
18+
};
19+
20+
module.exports.sendActivationEmail = (to, activationCode) => {
21+
const mailOptions = {
22+
from: 'Topkek <[email protected]>',
23+
to,
24+
subject: 'Email verification',
25+
text: `Please click on the link below to activate your account:\n\n ${makeActivationURL(
26+
activationCode
27+
)}`
28+
};
29+
30+
transporter.sendMail(mailOptions, (err, info) => {
31+
if (err) {
32+
console.log(err);
33+
}
34+
console.log(`Message sent: ${info.response}`);
35+
});
36+
};

api/index.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
const express = require('express');
2-
3-
const app = express.Router();
4-
5-
// routes
6-
const createAccount = require('./createAccount.js');
7-
const login = require('./login.js');
8-
const unauthorized = require('./unauthorized.js');
9-
const logout = require('./logout.js');
10-
const logged = require('./isLoggedIn.js');
11-
const activate = require('./activate.js');
12-
const blogPosts = require('./blogPosts.js');
13-
const upload = require('./upload.js');
14-
15-
app.use(login);
16-
app.use(createAccount);
17-
app.use(activate);
18-
app.use(blogPosts);
19-
//app.use(unauthorized);
20-
app.use(logout);
21-
app.use(logged);
22-
app.use(upload);
23-
24-
module.exports = app;
1+
const express = require('express');
2+
3+
const app = express.Router();
4+
5+
// routes
6+
const createAccount = require('./createAccount.js');
7+
const login = require('./login.js');
8+
// const unauthorized = require('./unauthorized.js');
9+
const logged = require('./isLoggedIn.js');
10+
const activate = require('./activate.js');
11+
const blogPosts = require('./blogPosts.js');
12+
const upload = require('./upload.js');
13+
14+
app.use(login);
15+
app.use(createAccount);
16+
app.use(activate);
17+
app.use(blogPosts);
18+
// app.use(unauthorized);
19+
app.use(logged);
20+
app.use(upload);
21+
22+
module.exports = app;

api/isLoggedIn.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
const express = require('express');
2-
3-
const app = express.Router();
4-
5-
app.all('/logged', (req, res) => {
6-
const user = req.session.currentUser;
7-
res.send(user);
8-
});
9-
10-
module.exports = app;
1+
const fs = require('fs');
2+
const express = require('express');
3+
const jwt = require('jsonwebtoken');
4+
5+
const app = express.Router();
6+
7+
const cert = fs.readFileSync('./keys/cert.pem');
8+
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);
18+
}
19+
);
20+
});
21+
22+
module.exports = app;

0 commit comments

Comments
 (0)