wikijs-fork/server/controllers/auth.js

79 lines
1.9 KiB
JavaScript
Raw Normal View History

/* global WIKI */
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')
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 })
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
/**
* Register form
*/
router.get('/register', async (req, res, next) => {
2018-12-24 22:38:34 +00:00
_.set(res.locals, 'pageMeta.title', 'Register')
const localStrg = await WIKI.models.authentication.getStrategy('local')
if (localStrg.selfRegistration) {
res.render('register')
} else {
next(new WIKI.Error.AuthRegistrationDisabled())
}
})
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