fix: legacy login errors + logout button
This commit is contained in:
		@@ -42,20 +42,28 @@ body {
 | 
				
			|||||||
    top: 0;
 | 
					    top: 0;
 | 
				
			||||||
    left: 0;
 | 
					    left: 0;
 | 
				
			||||||
    width: 100%;
 | 
					    width: 100%;
 | 
				
			||||||
    background-color: mc('red', '700');
 | 
					    background-color: mc('grey', '800');
 | 
				
			||||||
    text-align: center;
 | 
					    text-align: center;
 | 
				
			||||||
    color: mc('red', '50');
 | 
					    color: mc('grey', '50');
 | 
				
			||||||
    height: 64px;
 | 
					    height: 64px;
 | 
				
			||||||
    display: flex;
 | 
					    display: flex;
 | 
				
			||||||
    align-items: center;
 | 
					    align-items: center;
 | 
				
			||||||
    justify-content: center;
 | 
					    justify-content: center;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    a {
 | 
					    a {
 | 
				
			||||||
      color: #FFF;
 | 
					      color: mc('red', '200');
 | 
				
			||||||
      margin-left: 5px;
 | 
					      margin-left: 5px;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  &-error {
 | 
				
			||||||
 | 
					    background-color: mc('red', '500');
 | 
				
			||||||
 | 
					    color: #FFF;
 | 
				
			||||||
 | 
					    padding: 5px;
 | 
				
			||||||
 | 
					    border-radius: 5px;
 | 
				
			||||||
 | 
					    margin-bottom: 2rem;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  &-dialog {
 | 
					  &-dialog {
 | 
				
			||||||
    width: 650px;
 | 
					    width: 650px;
 | 
				
			||||||
    background-color: mc('grey', '100');
 | 
					    background-color: mc('grey', '100');
 | 
				
			||||||
@@ -171,6 +179,13 @@ body {
 | 
				
			|||||||
      text-decoration: none;
 | 
					      text-decoration: none;
 | 
				
			||||||
      color: #FFF;
 | 
					      color: #FFF;
 | 
				
			||||||
      transition: color .3s ease;
 | 
					      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 {
 | 
					      &:hover {
 | 
				
			||||||
        color: mc('blue', '500');
 | 
					        color: mc('blue', '500');
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,8 +6,6 @@ const BruteKnex = require('brute-knex')
 | 
				
			|||||||
const router = express.Router()
 | 
					const router = express.Router()
 | 
				
			||||||
const moment = require('moment')
 | 
					const moment = require('moment')
 | 
				
			||||||
const _ = require('lodash')
 | 
					const _ = require('lodash')
 | 
				
			||||||
const fs = require('fs-extra')
 | 
					 | 
				
			||||||
const path = require('path')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
const bruteforce = new ExpressBrute(new BruteKnex({
 | 
					const bruteforce = new ExpressBrute(new BruteKnex({
 | 
				
			||||||
  createTable: true,
 | 
					  createTable: true,
 | 
				
			||||||
@@ -28,32 +26,9 @@ router.get('/login', async (req, res, next) => {
 | 
				
			|||||||
  _.set(res.locals, 'pageMeta.title', 'Login')
 | 
					  _.set(res.locals, 'pageMeta.title', 'Login')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) {
 | 
					  if (req.query.legacy || req.get('user-agent').indexOf('Trident') >= 0) {
 | 
				
			||||||
    const strategies = await WIKI.models.authentication.query().select('key', 'selfRegistration').where({ isEnabled: true })
 | 
					    const { formStrategies, socialStrategies } = await WIKI.models.authentication.getStrategiesForLegacyClient()
 | 
				
			||||||
    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
 | 
					 | 
				
			||||||
          })
 | 
					 | 
				
			||||||
        })
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    res.render('legacy/login', {
 | 
					    res.render('legacy/login', {
 | 
				
			||||||
 | 
					      err: false,
 | 
				
			||||||
      formStrategies,
 | 
					      formStrategies,
 | 
				
			||||||
      socialStrategies
 | 
					      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.cookie('jwt', authResult.jwt, { expires: moment().add(1, 'y').toDate() })
 | 
				
			||||||
      res.redirect('/')
 | 
					      res.redirect('/')
 | 
				
			||||||
    } catch (err) {
 | 
					    } catch (err) {
 | 
				
			||||||
      res.render('legacy/login')
 | 
					      const { formStrategies, socialStrategies } = await WIKI.models.authentication.getStrategiesForLegacyClient()
 | 
				
			||||||
 | 
					      res.render('legacy/login', {
 | 
				
			||||||
 | 
					        err,
 | 
				
			||||||
 | 
					        formStrategies,
 | 
				
			||||||
 | 
					        socialStrategies
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    res.redirect('/login')
 | 
					    res.redirect('/login')
 | 
				
			||||||
@@ -121,6 +101,7 @@ router.post('/login', bruteforce.prevent, async (req, res, next) => {
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
router.get('/logout', function (req, res) {
 | 
					router.get('/logout', function (req, res) {
 | 
				
			||||||
  req.logout()
 | 
					  req.logout()
 | 
				
			||||||
 | 
					  res.clearCookie('jwt')
 | 
				
			||||||
  res.redirect('/')
 | 
					  res.redirect('/')
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -200,7 +200,7 @@ router.get('/*', async (req, res, next) => {
 | 
				
			|||||||
          if (_.isString(page.toc)) {
 | 
					          if (_.isString(page.toc)) {
 | 
				
			||||||
            page.toc = JSON.parse(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 {
 | 
					        } else {
 | 
				
			||||||
          res.render('page', { page, sidebar, injectCode })
 | 
					          res.render('page', { page, sidebar, injectCode })
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -44,6 +44,38 @@ module.exports = class Authentication extends Model {
 | 
				
			|||||||
    })), ['key'])
 | 
					    })), ['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() {
 | 
					  static async refreshStrategiesFromDisk() {
 | 
				
			||||||
    let trx
 | 
					    let trx
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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-deprecated Your browser is outdated. Upgrade to a #[a(href='https://bestvpn.org/outdatedbrowser/en', rel='nofollow') modern browser].
 | 
				
			||||||
    .login
 | 
					    .login
 | 
				
			||||||
      .login-dialog
 | 
					      .login-dialog
 | 
				
			||||||
 | 
					        if err
 | 
				
			||||||
 | 
					          .login-error= err.message
 | 
				
			||||||
        form(method='post', action='/login')
 | 
					        form(method='post', action='/login')
 | 
				
			||||||
          h1= config.title
 | 
					          h1= config.title
 | 
				
			||||||
          select(name='strategy')
 | 
					          select(name='strategy')
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,8 +12,12 @@ block body
 | 
				
			|||||||
      span.header-title= siteConfig.title
 | 
					      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-deprecated Your browser is outdated. Upgrade to a #[a(href='https://bestvpn.org/outdatedbrowser/en', rel='nofollow') modern browser].
 | 
				
			||||||
      span.header-login
 | 
					      span.header-login
 | 
				
			||||||
        a(href='/login')
 | 
					        if !isAuthenticated
 | 
				
			||||||
          i.material-icons account_circle
 | 
					          a(href='/login', title='Login')
 | 
				
			||||||
 | 
					            i.material-icons account_circle
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					          a(href='/logout', title='Logout')
 | 
				
			||||||
 | 
					            i.material-icons logout
 | 
				
			||||||
    .main
 | 
					    .main
 | 
				
			||||||
      .sidebar
 | 
					      .sidebar
 | 
				
			||||||
        each navItem in sidebar
 | 
					        each navItem in sidebar
 | 
				
			||||||
@@ -30,10 +34,10 @@ block body
 | 
				
			|||||||
          .page-header-left
 | 
					          .page-header-left
 | 
				
			||||||
            h1= page.title
 | 
					            h1= page.title
 | 
				
			||||||
            h2= page.description
 | 
					            h2= page.description
 | 
				
			||||||
          .page-header-right
 | 
					          //- .page-header-right
 | 
				
			||||||
            .page-header-right-title Last edited by
 | 
					          //-   .page-header-right-title Last edited by
 | 
				
			||||||
            .page-header-right-author= page.authorName
 | 
					          //-   .page-header-right-author= page.authorName
 | 
				
			||||||
            .page-header-right-updated= page.updatedAt
 | 
					          //-   .page-header-right-updated= page.updatedAt
 | 
				
			||||||
        .page-contents
 | 
					        .page-contents
 | 
				
			||||||
          .contents
 | 
					          .contents
 | 
				
			||||||
            div!= page.render
 | 
					            div!= page.render
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user