From 287fa13b3e600b2340895a5463a288bf08101bb5 Mon Sep 17 00:00:00 2001 From: Rubén Beltrán del Río Date: Thu, 19 Jan 2017 00:25:01 -0600 Subject: Add Login (#2) * Add dependencies * Add barebones app * Ignore .DS_Store * Add base static assets * Expose port on docker * Ignore env files * Add instructions to use env.dist * Add twitter configs * Use latest node version * Read env file properly * Add dependencies to start parsing twitter * Add helper for twitter login operations * Add handler for titter login routes * Use twitter handler * Add JWT related keys * Add jwt dependencies * Move index static file * Rename twitter handler to auth, add jwt support * Add new yarn lock * Add instructions for twitter * Remove twitter image * Fix return value lint errors * Ignore require-yield errors in linter --- lib/dasein.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) (limited to 'lib/dasein.js') diff --git a/lib/dasein.js b/lib/dasein.js index 8de32c8..38654a4 100644 --- a/lib/dasein.js +++ b/lib/dasein.js @@ -1,13 +1,106 @@ 'use strict'; +const Koa = require('koa'); +const KoaJwt = require('koa-jwt'); +const KoaStatic = require('koa-static'); +const KoaRoute = require('koa-route'); + +const AuthHandler = require('./handlers/auth'); + const internals = {}; +internals.k401Location = '/401.html'; +internals.kMainLocation = '/'; + module.exports = internals.Dasein = class Dasein { + constructor(config) { + + Object.assign(this, config); + } + run() { - console.log('OK'); + this._initializeServer(); + this._startServer(); + this._printBanner(); return Promise.resolve(); } + + _initializeServer() { + + this._app = Koa(); + + this._app.keys = this.cookieKeys; + + this._app.use(KoaStatic(this.staticDirectory)); + + // Redirect all 401s to the 401 static page + + this._app.use(function * (next) { + + try { + yield next; + } + catch (err) { + if (err.status === 401) { + return this.redirect(internals.k401Location); + } + + throw err; + } + }); + + this._app.use(KoaJwt({ + secret: this.jwt.secret, + passthrough: true, + cookie: this.jwt.cookieName + })); + + // Handlers for Twitter Auth Related Routes + + const authHandler = new AuthHandler({ + hostname: this.hostname, + jwt: this.jwt, + twitter: this.twitter + }); + this._app.use(KoaRoute.get('/login', authHandler.login())); + this._app.use(KoaRoute.get('/login-callback', authHandler.callback())); + this._app.use(KoaRoute.get('/logout', authHandler.logout())); + + // The index + + this._app.use(function * () { + + if (this.state.user) { + this.body = ` Hello ${this.state.user.screen_name}`; + return; + } + + this.body = 'Go to /login to login'; + return; + }); + } + + _startServer() { + + this._app.listen(this.port); + } + + // Prints the banner. + _printBanner() { + + console.log(' .'); + console.log(' /'); + console.log(' +-----+'); + console.log(` | o o | - Listening Gladly, Try me on port: ${this.port}`); + console.log(' +-----+'); + console.log(' +---------+'); + console.log(' /| [][] |\\'); + console.log(' || | |'); + console.log(' || | \\c'); + console.log(' ^+---------+'); + console.log(' (.) '); + } }; -- cgit