You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

  1. /* global WIKI */
  2. const express = require('express')
  3. const router = express.Router()
  4. const moment = require('moment')
  5. const _ = require('lodash')
  6. /**
  7. * Login form
  8. */
  9. router.get('/login', function (req, res, next) {
  10. _.set(res.locals, 'pageMeta.title', 'Login')
  11. res.render('login')
  12. })
  13. /**
  14. * Logout
  15. */
  16. router.get('/logout', function (req, res) {
  17. req.logout()
  18. res.redirect('/')
  19. })
  20. /**
  21. * Register form
  22. */
  23. router.get('/register', async (req, res, next) => {
  24. _.set(res.locals, 'pageMeta.title', 'Register')
  25. const localStrg = await WIKI.models.authentication.getStrategy('local')
  26. if (localStrg.selfRegistration) {
  27. res.render('register')
  28. } else {
  29. next(new WIKI.Error.AuthRegistrationDisabled())
  30. }
  31. })
  32. /**
  33. * Verify
  34. */
  35. router.get('/verify/:token', async (req, res, next) => {
  36. const usr = await WIKI.models.userKeys.validateToken({ kind: 'verify', token: req.params.token })
  37. await WIKI.models.users.query().patch({ isVerified: true }).where('id', usr.id)
  38. const result = await WIKI.models.users.refreshToken(usr)
  39. res.cookie('jwt', result.token, { expires: moment().add(1, 'years').toDate() })
  40. res.redirect('/')
  41. })
  42. /**
  43. * JWT Public Endpoints
  44. */
  45. router.get('/.well-known/jwk.json', function (req, res, next) {
  46. res.json(WIKI.config.certs.jwk)
  47. })
  48. router.get('/.well-known/jwk.pem', function (req, res, next) {
  49. res.send(WIKI.config.certs.public)
  50. })
  51. module.exports = router