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')
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Login form
|
|
|
|
*/
|
2017-02-09 01:52:37 +00:00
|
|
|
router.get('/login', function (req, res, next) {
|
2018-08-11 22:16:56 +00:00
|
|
|
res.render('login')
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
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) => {
|
|
|
|
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
|