Created
February 19, 2016 17:38
-
-
Save marr/3783b5e1697f87c367dc to your computer and use it in GitHub Desktop.
This file contains 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
const Koa = require('koa') | |
const app = new Koa() | |
// trust proxy | |
app.proxy = true | |
// sessions | |
const convert = require('koa-convert') | |
const session = require('koa-generic-session') | |
app.keys = ['your-session-secret'] | |
app.use(convert(session())) | |
// body parser | |
const bodyParser = require('koa-bodyparser') | |
app.use(bodyParser()) | |
// authentication | |
require('./authn') | |
const passport = require('koa-passport') | |
app.use(passport.initialize()) | |
app.use(passport.session()) | |
// routes | |
const fs = require('fs') | |
const route = require('koa-route') | |
app.use(route.get('/logout', function(ctx) { | |
ctx.logout() | |
ctx.redirect('/login') | |
})) | |
app.use(route.get('/login', | |
passport.authenticate('oauth2') | |
)) | |
app.use(route.get('/oauth/callback', | |
passport.authenticate('oauth2', { | |
failureRedirect: '/login', | |
successRedirect: '/' | |
}) | |
)) | |
// Require authentication for now | |
app.use(function(ctx, next) { | |
if (ctx.isAuthenticated()) { | |
return next() | |
} else { | |
ctx.redirect('/login') | |
} | |
}) | |
app.use(route.get('/api/token', function(ctx) { | |
const user = ctx.req.user | |
ctx.body = { token: user.accessToken } | |
})) | |
// start server | |
const port = process.env.PORT || 3000 | |
app.listen(port, () => console.log('Server listening on', port)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment