diff options
| author | Rubén Beltrán del Río <ben@nsovocal.com> | 2017-01-19 00:25:01 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-01-19 00:25:01 -0600 |
| commit | 287fa13b3e600b2340895a5463a288bf08101bb5 (patch) | |
| tree | 68b35cdd1bea40a6d51665ac7ed6dc4044eeda49 /lib/dasein.js | |
| parent | cc69fef4b7e1587a91a0603d4bd1a46d0b133dd5 (diff) | |
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
Diffstat (limited to 'lib/dasein.js')
| -rw-r--r-- | lib/dasein.js | 95 |
1 files changed, 94 insertions, 1 deletions
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 = `<img src="${this.state.user.profile_image_url_https}"> 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(' (.) '); + } }; |