From d546695143807f2776939138125a786cefac4276 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 13 Jul 2019 16:18:43 -0400 Subject: [PATCH] fix: legacy login errors + logout button --- client/scss/legacy.scss | 21 ++++++++++++++++--- server/controllers/auth.js | 37 ++++++++------------------------- server/controllers/common.js | 2 +- server/models/authentication.js | 32 ++++++++++++++++++++++++++++ server/views/legacy/login.pug | 2 ++ server/views/legacy/page.pug | 16 ++++++++------ 6 files changed, 72 insertions(+), 38 deletions(-) diff --git a/client/scss/legacy.scss b/client/scss/legacy.scss index 7b62b735..584bebfd 100644 --- a/client/scss/legacy.scss +++ b/client/scss/legacy.scss @@ -42,20 +42,28 @@ body { top: 0; left: 0; width: 100%; - background-color: mc('red', '700'); + background-color: mc('grey', '800'); text-align: center; - color: mc('red', '50'); + color: mc('grey', '50'); height: 64px; display: flex; align-items: center; justify-content: center; a { - color: #FFF; + color: mc('red', '200'); margin-left: 5px; } } + &-error { + background-color: mc('red', '500'); + color: #FFF; + padding: 5px; + border-radius: 5px; + margin-bottom: 2rem; + } + &-dialog { width: 650px; background-color: mc('grey', '100'); @@ -171,6 +179,13 @@ body { text-decoration: none; color: #FFF; transition: color .3s ease; + border-radius: 50%; + background-color: mc('grey', '900'); + display: flex; + width: 40px; + height: 40px; + justify-content: center; + align-items: center; &:hover { color: mc('blue', '500'); diff --git a/server/controllers/auth.js b/server/controllers/auth.js index 9eaa7ff9..de8b0126 100644 --- a/server/controllers/auth.js +++ b/server/controllers/auth.js @@ -6,8 +6,6 @@ const BruteKnex = require('brute-knex') const router = express.Router() const moment = require('moment') const _ = require('lodash') -const fs = require('fs-extra') -const path = require('path') const bruteforce = new ExpressBrute(new BruteKnex({ createTable: true, @@ -28,32 +26,9 @@ router.get('/login', async (req, res, next) => { _.set(res.locals, 'pageMeta.title', 'Login') if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) { - const strategies = await WIKI.models.authentication.query().select('key', 'selfRegistration').where({ isEnabled: true }) - let formStrategies = [] - let socialStrategies = [] - - // TODO: Let's refactor that at some point... - for (let stg of strategies) { - const stgInfo = _.find(WIKI.data.authentication, ['key', stg.key]) || {} - if (stgInfo.useForm) { - formStrategies.push({ - key: stg.key, - title: stgInfo.title - }) - } else { - socialStrategies.push({ - ...stgInfo, - ...stg, - icon: await fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${stg.key}.svg`), 'utf8').catch(err => { - if (err.code === 'ENOENT') { - return null - } - throw err - }) - }) - } - } + const { formStrategies, socialStrategies } = await WIKI.models.authentication.getStrategiesForLegacyClient() res.render('legacy/login', { + err: false, formStrategies, socialStrategies }) @@ -109,7 +84,12 @@ router.post('/login', bruteforce.prevent, async (req, res, next) => { res.cookie('jwt', authResult.jwt, { expires: moment().add(1, 'y').toDate() }) res.redirect('/') } catch (err) { - res.render('legacy/login') + const { formStrategies, socialStrategies } = await WIKI.models.authentication.getStrategiesForLegacyClient() + res.render('legacy/login', { + err, + formStrategies, + socialStrategies + }) } } else { res.redirect('/login') @@ -121,6 +101,7 @@ router.post('/login', bruteforce.prevent, async (req, res, next) => { */ router.get('/logout', function (req, res) { req.logout() + res.clearCookie('jwt') res.redirect('/') }) diff --git a/server/controllers/common.js b/server/controllers/common.js index 5a891f64..9465e76c 100644 --- a/server/controllers/common.js +++ b/server/controllers/common.js @@ -200,7 +200,7 @@ router.get('/*', async (req, res, next) => { if (_.isString(page.toc)) { page.toc = JSON.parse(page.toc) } - res.render('legacy/page', { page, sidebar, injectCode }) + res.render('legacy/page', { page, sidebar, injectCode, isAuthenticated: req.user && req.user.id !== 2 }) } else { res.render('page', { page, sidebar, injectCode }) } diff --git a/server/models/authentication.js b/server/models/authentication.js index 80ec3ffb..6d13a772 100644 --- a/server/models/authentication.js +++ b/server/models/authentication.js @@ -44,6 +44,38 @@ module.exports = class Authentication extends Model { })), ['key']) } + static async getStrategiesForLegacyClient() { + const strategies = await WIKI.models.authentication.query().select('key', 'selfRegistration').where({ isEnabled: true }) + let formStrategies = [] + let socialStrategies = [] + + for (let stg of strategies) { + const stgInfo = _.find(WIKI.data.authentication, ['key', stg.key]) || {} + if (stgInfo.useForm) { + formStrategies.push({ + key: stg.key, + title: stgInfo.title + }) + } else { + socialStrategies.push({ + ...stgInfo, + ...stg, + icon: await fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${stg.key}.svg`), 'utf8').catch(err => { + if (err.code === 'ENOENT') { + return null + } + throw err + }) + }) + } + } + + return { + formStrategies, + socialStrategies + } + } + static async refreshStrategiesFromDisk() { let trx try { diff --git a/server/views/legacy/login.pug b/server/views/legacy/login.pug index de870b58..9952804e 100644 --- a/server/views/legacy/login.pug +++ b/server/views/legacy/login.pug @@ -5,6 +5,8 @@ block body .login-deprecated Your browser is outdated. Upgrade to a #[a(href='https://bestvpn.org/outdatedbrowser/en', rel='nofollow') modern browser]. .login .login-dialog + if err + .login-error= err.message form(method='post', action='/login') h1= config.title select(name='strategy') diff --git a/server/views/legacy/page.pug b/server/views/legacy/page.pug index dcc58d53..f10a9b41 100644 --- a/server/views/legacy/page.pug +++ b/server/views/legacy/page.pug @@ -12,8 +12,12 @@ block body span.header-title= siteConfig.title span.header-deprecated Your browser is outdated. Upgrade to a #[a(href='https://bestvpn.org/outdatedbrowser/en', rel='nofollow') modern browser]. span.header-login - a(href='/login') - i.material-icons account_circle + if !isAuthenticated + a(href='/login', title='Login') + i.material-icons account_circle + else + a(href='/logout', title='Logout') + i.material-icons logout .main .sidebar each navItem in sidebar @@ -30,10 +34,10 @@ block body .page-header-left h1= page.title h2= page.description - .page-header-right - .page-header-right-title Last edited by - .page-header-right-author= page.authorName - .page-header-right-updated= page.updatedAt + //- .page-header-right + //- .page-header-right-title Last edited by + //- .page-header-right-author= page.authorName + //- .page-header-right-updated= page.updatedAt .page-contents .contents div!= page.render