diff --git a/middlewares/publicRoute.js b/middlewares/publicRoute.js new file mode 100644 index 0000000..b77b899 --- /dev/null +++ b/middlewares/publicRoute.js @@ -0,0 +1,10 @@ +const verifyAuth = require('./verifyAuth') + +const publicMiddleware = () => process.env.LISTS_PUBLIC === 'true' + ? (req, res, next) => { + if (!req.user) req.user = { _id: 'Unknown' } + next() + } + : verifyAuth() + +module.exports = publicMiddleware diff --git a/routes/index.js b/routes/index.js index 3bd1a40..7a34263 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,4 +1,4 @@ -const verifyAuth = require('../middlewares/verifyAuth') +const publicRoute = require('../middlewares/publicRoute') const express = require('express') const path = require('path') @@ -16,7 +16,7 @@ module.exports = ({ db, config }) => { next() } }, - verifyAuth(), + publicRoute(), (req, res) => { res.redirect('/wishlist') } diff --git a/routes/wishlist/index.js b/routes/wishlist/index.js index 1cb9db6..684dad7 100644 --- a/routes/wishlist/index.js +++ b/routes/wishlist/index.js @@ -1,4 +1,5 @@ const verifyAuth = require('../../middlewares/verifyAuth') +const publicRoute = require('../../middlewares/publicRoute') const getProductName = require('get-product-name') const express = require('express') const config = require('../../config') @@ -29,7 +30,7 @@ const ValidURL = (string) => { // Ty SO module.exports = (db) => { const router = express.Router() - router.get('/', verifyAuth(), async (req, res) => { + router.get('/', publicRoute(), async (req, res) => { const docs = await db.allDocs({ include_docs: true }) if (process.env.SINGLE_LIST === 'true') { for (const row of docs.rows) { @@ -39,7 +40,7 @@ module.exports = (db) => { res.render('wishlists', { title: 'Wishlists', users: docs.rows, totals }) }) - router.get('/:user', verifyAuth(), async (req, res) => { + router.get('/:user', publicRoute(), async (req, res) => { try { const dbUser = await db.get(req.params.user) if (process.env.SINGLE_LIST === 'true') { diff --git a/views/includes/navbar.pug b/views/includes/navbar.pug index a85e8c1..a7afe8a 100644 --- a/views/includes/navbar.pug +++ b/views/includes/navbar.pug @@ -20,16 +20,19 @@ nav.navbar.is-fixed-top(role='navigation', aria-label='main navigation',style='b .navbar-menu#navBarMenu .navbar-start if req.isAuthenticated() - .navbar-item.has-dropdown.is-hoverable - a.navbar-link= req.user._id - .navbar-dropdown - +navBarLink(`${_CC.config.base}wishlist/${req.user._id}`, 'My Wishlist') - +navBarLink(`${_CC.config.base}profile`, 'Profile') - if req.user.admin - +navBarLink(`${_CC.config.base}admin-settings`, 'Admin settings') - hr.navbar-divider - .navbar-item - form#logoutForm(action=`${_CC.config.base}logout`, method='POST') - button.button.is-warning(type='submit') Log Out - //-+navBarLink('javascript:document.getElementById("logoutForm").submit()', 'Log Out') + if req.user._id === 'Unknown' + .navbar-item + a.button.is-primary(href='/login') Log In + else + .navbar-item.has-dropdown.is-hoverable + a.navbar-link= req.user._id + .navbar-dropdown + +navBarLink(`${_CC.config.base}wishlist/${req.user._id}`, 'My Wishlist') + +navBarLink(`${_CC.config.base}profile`, 'Profile') + if req.user.admin + +navBarLink(`${_CC.config.base}admin-settings`, 'Admin settings') + hr.navbar-divider + .navbar-item + form#logoutForm(action=`${_CC.config.base}logout`, method='POST') + button.button.is-warning(type='submit') Log Out script(src=`${_CC.config.base}js/nav.js`) diff --git a/views/wishlists.pug b/views/wishlists.pug index 89e6d76..ba96ad1 100644 --- a/views/wishlists.pug +++ b/views/wishlists.pug @@ -2,12 +2,13 @@ extends layout.pug block content ul.noStyle.noLeftMargin - li - a(href=`${_CC.config.base}wishlist/${req.user._id}`) - .box - span=req.user._id - span : ???/??? - progress.progress(value=1, max=1) + if req.user._id !== 'Unknown' + li + a(href=`${_CC.config.base}wishlist/${req.user._id}`) + .box + span=req.user._id + span : ???/??? + progress.progress(value=1, max=1) each user in users if req.user._id !== user.id li