From 43a0e72c9955fd2900492530ab283d083b617ba8 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Mon, 19 Oct 2020 17:58:08 -0500 Subject: Add about and signup --- src/server.js | 35 ++++++++++++++++++++++++++++++++++- src/templates.js | 3 ++- src/templates/about.html | 34 ++++++++++++++++++++++++++++++++++ src/templates/expected.html | 1 + src/templates/goals.html | 1 + src/templates/ledger.html | 1 + src/templates/login.html | 29 +---------------------------- src/templates/sign-up.html | 23 +++++++++++++++++++++++ src/templates/summary.html | 1 + 9 files changed, 98 insertions(+), 30 deletions(-) create mode 100644 src/templates/about.html create mode 100644 src/templates/sign-up.html (limited to 'src') diff --git a/src/server.js b/src/server.js index 5d6cf9f..541f467 100644 --- a/src/server.js +++ b/src/server.js @@ -26,7 +26,8 @@ function hashWithSalt(password, salt) { function setUpRoutes(models, jwtFunctions, database, templates) { // Authentication routine server.use(async function (req, res, next) { - if (!req.path.toLowerCase().startsWith("/login")) { + let path = req.path.toLowerCase(); + if (!path.startsWith("/login")) { let cookie = req.cookies.authorization if (!cookie) { console.debug("Redirecting to login - no cookie") @@ -58,10 +59,18 @@ function setUpRoutes(models, jwtFunctions, database, templates) { server.use('/static', express.static(path.join(__dirname, '/static'))) server.get('/', (req, res) => res.redirect("/ledger")) + server.get('/about', (req, res) => { + let body = templates["about"]({}); + res.status(200).send(body) + }) server.get('/login', (req, res) => { let body = templates["login"]({}); res.status(200).send(body) }) + server.get('/login/signup', async (req, res) => { + let body = templates["signup"]({}); + res.status(200).send(body) + }) server.get('/ledger', async (req, res) => { var ledger = await database.query(`SELECT * FROM transactions WHERE username = '${res.locals.user.username}' ORDER BY \`when\` DESC`, { type: database.QueryTypes.SELECT }) ledger.forEach((element, i) => { @@ -134,6 +143,30 @@ function setUpRoutes(models, jwtFunctions, database, templates) { res.redirect('/login'); } }) + server.post('/login/signup', async (req, res) => { + if(req.body.code != config.signup_code){ + console.debug("Redirecting to signup - bad code") + res.redirect('/login/signup'); + return; + } + const user = await models.users.findOne({ where: { username: req.body.username } }) + if(user){ + console.debug("Redirecting to signup - user already exists") + res.redirect('/login/signup'); + return; + } + let salt = crypto.randomBytes(32).toString("Base64"); + let password = req.body.password + const hash = hashWithSalt(password, salt) + let new_user = { + username: req.body.username, + password: hash, + salt: salt + } + await models.users.create(new_user); + console.debug("Created account - log in") + res.redirect("/login") + }) server.post(`/transaction`, async (req, res, next) => { try { let item = req.body; diff --git a/src/templates.js b/src/templates.js index f7bfdfb..28435f8 100644 --- a/src/templates.js +++ b/src/templates.js @@ -9,8 +9,9 @@ function loadTemplate(templates, name, filepath){ function setUpTemplates(){ let templates = {}; - + loadTemplate(templates, "about", path.join(__dirname, 'templates/about.html')) loadTemplate(templates, "login", path.join(__dirname, 'templates/login.html')) + loadTemplate(templates, "signup", path.join(__dirname, 'templates/sign-up.html')) loadTemplate(templates, "ledger", path.join(__dirname, 'templates/ledger.html')) loadTemplate(templates, "ledger-edit", path.join(__dirname, 'templates/ledger-edit.html')) loadTemplate(templates, "goals", path.join(__dirname, 'templates/goals.html')) diff --git a/src/templates/about.html b/src/templates/about.html new file mode 100644 index 0000000..314545e --- /dev/null +++ b/src/templates/about.html @@ -0,0 +1,34 @@ + + + + + Mark's Database - Login + + + + + + +
+ + + + + +
+
+

About

+

Welcome to the alpha budget app! You've been given exclusive access to this secret trial

+

Here is how I use this site

+
    +
  1. Expenses go in Ledger. Income also goes in the ledger as negative
  2. +
  3. The category I use for big categories: bills, groceries, restaurants
  4. +
  5. Inside tags (which aren't csv yet) I have specifics: internet, coffee, gas
  6. +
  7. Summary gives some view of where money is going and coming from
  8. +
  9. Expected is what I expect in an out, for a period of days. For example, I pay internet every 31 days, and renew my domain every 365 days
  10. +
  11. Goals can be used to subdivide net savings into categories. It is still W.I.P.
  12. +
+
+ + + \ No newline at end of file diff --git a/src/templates/expected.html b/src/templates/expected.html index 99bf6fd..64841a5 100644 --- a/src/templates/expected.html +++ b/src/templates/expected.html @@ -14,6 +14,7 @@ +
diff --git a/src/templates/goals.html b/src/templates/goals.html index b617a6e..b5dbe42 100644 --- a/src/templates/goals.html +++ b/src/templates/goals.html @@ -14,6 +14,7 @@ + diff --git a/src/templates/ledger.html b/src/templates/ledger.html index f5c46de..543ee3a 100644 --- a/src/templates/ledger.html +++ b/src/templates/ledger.html @@ -15,6 +15,7 @@ + diff --git a/src/templates/login.html b/src/templates/login.html index f090108..dcf8f8e 100644 --- a/src/templates/login.html +++ b/src/templates/login.html @@ -2,37 +2,10 @@ - Mark's Database - Login + Login - diff --git a/src/templates/sign-up.html b/src/templates/sign-up.html new file mode 100644 index 0000000..81789bf --- /dev/null +++ b/src/templates/sign-up.html @@ -0,0 +1,23 @@ + + + + + Sign Up + + + + + + +
+

Sign up!

+ + + + + +
+ + + + \ No newline at end of file diff --git a/src/templates/summary.html b/src/templates/summary.html index bdc2f44..a998be9 100644 --- a/src/templates/summary.html +++ b/src/templates/summary.html @@ -14,6 +14,7 @@ +
-- cgit v1.2.3