Last active
          August 29, 2015 14:15 
        
      - 
      
 - 
        
Save chrisveness/1e4eb603267ebbc32ca9 to your computer and use it in GitHub Desktop.  
    koa-flash fails when session domain opts set
  
        
  
    
      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
    
  
  
    
  | var koa = require('koa'); | |
| var session = require('koa-session'); | |
| var flash = require('koa-flash'); | |
| var router = require('koa-router'); | |
| var handlebars = require("koa-handlebars"); | |
| var app = koa(); | |
| app.keys = ['foo']; | |
| app.use(session({ domain: '.localhost' }, app)); // THIS FAILS | |
| //app.use(session(app)); // THIS WORKS | |
| app.use(flash()); | |
| app.use(handlebars({ extension: ['html'], viewsDir: './', partialsDir: './' })); | |
| app.use(router(app)); | |
| var handler = {}; | |
| handler.get = function*() { | |
| var context = {}; | |
| if (this.flash) context.flashmsg = this.flash.flashmsg; | |
| console.log('handler.get flash', this.flash); | |
| yield this.render('flash', context); | |
| }; | |
| handler.post = function*() { | |
| this.flash = { flashmsg: 'This is a flash' }; | |
| console.log('handler.post flash', this.flash); | |
| this.redirect(this.url); | |
| }; | |
| var routes = function(app) { | |
| app.get('/flash', handler.get); | |
| app.post('/flash', handler.post); | |
| }; | |
| routes(app); | |
| app.listen(process.env.PORT||3000); | |
| console.log(process.version+' listening on port '+(process.env.PORT||3000)+' ('+app.env+')'); | 
  
    
      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
    
  
  
    
  | <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Flash test</title> | |
| <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css"> | |
| </head> | |
| <body> | |
| <h1>Flash test</h1> | |
| {{#if flashmsg}}<p>Flash message: {{flashmsg}}</p>{{/if}} | |
| <form method="post"> | |
| <button type="submit">Submit</button> | |
| </form> | |
| </body> | |
| </html> | 
  
    
      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
    
  
  
    
  | { | |
| "main": "app.js", | |
| "scripts": { | |
| "start": "nodemon --harmony -e js,json,html app.js" | |
| }, | |
| "dependencies": { | |
| "koa": "^0.17.0", | |
| "koa-flash": "^0.1.0", | |
| "koa-handlebars": "^0.5.0", | |
| "koa-router": "^3.8.0", | |
| "koa-session": "^3.1.0" | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Sorted! This was because cookies don’t work with ‘.localhost’ – see github.com/rickharrison/koa-flash/issues/5