Created
July 6, 2017 13:28
-
-
Save trezy/ae510d39f0569751860599152a00cff1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
/******************************************************************************\ | |
Module imports | |
\******************************************************************************/ | |
const { URL } = require('url') | |
const cookie = require('koa-cookie') | |
const next = require('next') | |
const path = require('path') | |
const request = require('request-promise-native') | |
const router = require('koa-router')() | |
const send = require('koa-send') | |
module.exports = function (nextjs, koa, config) { | |
/******************************************************************************\ | |
GET routes | |
\******************************************************************************/ | |
let handle = nextjs.getRequestHandler() | |
let authenticatedRoutes = [ | |
'/profile', | |
'/admin/*', | |
] | |
router.use(cookie.default()) | |
router.get(authenticatedRoutes, async (ctx, next) => { | |
if (ctx.cookie && ctx.cookie.access_token) { | |
await next() | |
} | |
console.log('request', ctx.request) | |
console.log('response', ctx.response) | |
nextjs.render(ctx.request, ctx.response, '/', { | |
attemptedDestination: ctx.route | |
}) | |
}) | |
router.get('*', async ctx => { | |
await handle(ctx.req, ctx.res) | |
ctx.respond = false | |
}) | |
koa.use(async (ctx, next) => { | |
ctx.res.statusCode = 200 | |
await next() | |
}) | |
/******************************************************************************\ | |
Attach the router to the app | |
\******************************************************************************/ | |
koa.use(router.routes()) | |
koa.use(router.allowedMethods()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment