Many changes

- Express -> Koa
- Babel for transforming 'import' into 'require'
- SQL done for adding request; just need to get the app to call it
This commit is contained in:
Daniel J. Summers
2017-09-19 22:20:17 -05:00
parent 270ac45e8c
commit 794a365f08
12 changed files with 1179 additions and 293 deletions

View File

@@ -1,31 +1,39 @@
'use strict'
const jwt = require('express-jwt')
const jwksRsa = require('jwks-rsa')
const config = require('../appsettings.json')
import jwt from 'koa-jwt'
import jwksRsa from 'jwks-rsa-koa'
import Router from 'koa-router'
// Authentication middleware. When used, the
// access token must exist and be verified against
// the Auth0 JSON Web Key Set
import appConfig from '../appsettings.json'
import journal from './journal'
import request from './request'
/** Authentication middleware to verify the access token against the Auth0 JSON Web Key Set */
const checkJwt = jwt({
// Dynamically provide a signing key
// based on the kid in the header and
// the singing keys provided by the JWKS endpoint.
secret: jwksRsa.expressJwtSecret({
secret: jwksRsa.koaJwt2Key({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${config.auth0.domain}/.well-known/jwks.json`
jwksUri: `https://${appConfig.auth0.domain}/.well-known/jwks.json`
}),
// Validate the audience and the issuer.
audience: config.auth0.clientId,
issuer: `https://${config.auth0.domain}/`,
audience: appConfig.auth0.clientId,
issuer: `https://${appConfig.auth0.domain}/`,
algorithms: ['RS256']
})
module.exports = {
mount: app => {
app.use('/api/journal', require('./journal')(checkJwt))
}
}
/** /api/journal routes */
const journalRoutes = journal(checkJwt)
/** /api/request routes */
const requestRoutes = request(checkJwt)
/** Combined router */
const router = new Router({ prefix: '/api' })
router.use('/journal', journalRoutes.routes(), journalRoutes.allowedMethods())
router.use('/request', requestRoutes.routes(), requestRoutes.allowedMethods())
export default router

View File

@@ -1,15 +1,16 @@
'use strict'
const Router = require('express-promise-router')
const db = require('../db')
import Router from 'koa-router'
import db from '../db'
module.exports = checkJwt => {
let router = new Router()
const router = new Router()
router.get('/', checkJwt, async (req, res) => {
const reqs = await db.request.journal(req.user.sub)
res.json(reqs)
export default function (checkJwt) {
router.get('/', checkJwt, async (ctx, next) => {
const reqs = await db.request.journal(ctx.state.user.sub)
ctx.body = reqs
return await next()
})
return router
}

18
src/api/routes/request.js Normal file
View File

@@ -0,0 +1,18 @@
'use strict'
import Router from 'koa-router'
import db from '../db'
const router = new Router()
export default function (checkJwt) {
router.post('/', checkJwt, async (ctx, next) => {
const newId = await db.request.addNew(ctx.state.user.sub, ctx.body.requestText)
ctx.body = { id: newId }
await next()
})
return router
}