2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-05-13 19:29:00 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
2018-12-24 06:03:10 +00:00
|
|
|
const moment = require('moment')
|
2018-12-24 22:38:34 +00:00
|
|
|
const _ = require('lodash')
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Login form
|
|
|
|
*/
|
2019-04-21 06:04:00 +00:00
|
|
|
router.get('/login', (req, res, next) => {
|
2018-12-24 22:38:34 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Login')
|
2018-08-11 22:16:56 +00:00
|
|
|
res.render('login')
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
2019-04-21 06:04:00 +00:00
|
|
|
router.get('/login/:strategy', async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const authResult = await WIKI.models.users.login({
|
|
|
|
strategy: req.params.strategy
|
|
|
|
}, { req, res })
|
|
|
|
} catch (err) {
|
|
|
|
next(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
router.get('/login/:strategy/callback', async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const authResult = await WIKI.models.users.login({
|
|
|
|
strategy: req.params.strategy
|
|
|
|
}, { req, res })
|
2019-04-22 01:43:33 +00:00
|
|
|
res.cookie('jwt', authResult.jwt, { expires: moment().add(1, 'y').toDate() })
|
|
|
|
res.redirect('/')
|
2019-04-21 06:04:00 +00:00
|
|
|
} catch (err) {
|
|
|
|
next(err)
|
|
|
|
}
|
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logout
|
|
|
|
*/
|
2017-02-09 01:52:37 +00:00
|
|
|
router.get('/logout', function (req, res) {
|
|
|
|
req.logout()
|
|
|
|
res.redirect('/')
|
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2018-12-17 05:51:52 +00:00
|
|
|
/**
|
|
|
|
* Register form
|
|
|
|
*/
|
2018-12-21 04:02:17 +00:00
|
|
|
router.get('/register', async (req, res, next) => {
|
2018-12-24 22:38:34 +00:00
|
|
|
_.set(res.locals, 'pageMeta.title', 'Register')
|
2018-12-21 04:02:17 +00:00
|
|
|
const localStrg = await WIKI.models.authentication.getStrategy('local')
|
|
|
|
if (localStrg.selfRegistration) {
|
|
|
|
res.render('register')
|
|
|
|
} else {
|
|
|
|
next(new WIKI.Error.AuthRegistrationDisabled())
|
|
|
|
}
|
2018-12-17 05:51:52 +00:00
|
|
|
})
|
|
|
|
|
2018-12-24 06:03:10 +00:00
|
|
|
/**
|
|
|
|
* Verify
|
|
|
|
*/
|
|
|
|
router.get('/verify/:token', async (req, res, next) => {
|
|
|
|
const usr = await WIKI.models.userKeys.validateToken({ kind: 'verify', token: req.params.token })
|
|
|
|
await WIKI.models.users.query().patch({ isVerified: true }).where('id', usr.id)
|
|
|
|
const result = await WIKI.models.users.refreshToken(usr)
|
|
|
|
res.cookie('jwt', result.token, { expires: moment().add(1, 'years').toDate() })
|
|
|
|
res.redirect('/')
|
|
|
|
})
|
|
|
|
|
2018-12-03 02:42:43 +00:00
|
|
|
/**
|
|
|
|
* JWT Public Endpoints
|
|
|
|
*/
|
|
|
|
router.get('/.well-known/jwk.json', function (req, res, next) {
|
|
|
|
res.json(WIKI.config.certs.jwk)
|
|
|
|
})
|
|
|
|
router.get('/.well-known/jwk.pem', function (req, res, next) {
|
|
|
|
res.send(WIKI.config.certs.public)
|
|
|
|
})
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
module.exports = router
|