|
@ -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 }) |
|
|
|
|
|
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', { |
|
|
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('/') |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|