rkusa / koa-passport-example

koa-passport usage example
https://github.com/rkusa/koa-passport
262 stars 74 forks source link

Incompatible with koa-mount - TypeError: Cannot read property 'store' of undefined at session #24

Open tkodev opened 6 years ago

tkodev commented 6 years ago

I'm working with koa-mount, looks like the problem is koa-session but I'm completely lost with this implementation with koa-passport. My code is like : server.js:

'use strict';

const koa = require("koa");
const application = require('./application.js')
const app = new koa();
const appPort = 3000;

// Mount application
const mount = require('koa-mount');
app.use(mount('/application', application));

// serve index.html
const serve = require('koa-static-server');
app.use(serve({rootDir: __dirname+'/test', rootPath: '/'}));

// init server
app.listen(appPort, function() {
    console.log(`Ready at port: ${appPort}`)
});

application.js:

// use strict code
"use strict";

const fs = require('fs');

// appFactory
const appFactory = () => {
    const Koa = require('koa')
    const app = new Koa()
    const serve = require('koa-static-server')
    const router = require('koa-router')();
    const session = require('koa-session')
    const bodyParser = require('koa-bodyparser')
    const passport = require('koa-passport')

    // trust proxy
    app.proxy = true

    // session
    app.keys = ['your-session-secret']
    app.use(session({}, app)) // <-- _problem occurs here_

    // body parser
    app.use(bodyParser())

    // passport
    require('./auth')
    app.use(passport.initialize())
    app.use(passport.session())

    // post login
    router.post('/login', passport.authenticate('local', {
        successRedirect: '/',
        failureRedirect: '/eos/'
    }));

    // post logout
    router.get('/logout', function(ctx) {
        if (ctx.isAuthenticated()) {
            ctx.logout()
        }
        ctx.redirect('/')
    });

    // admin route
    router.get('/', async(ctx) => {
        ctx.type = 'html'
        if (ctx.isAuthenticated()) {
            ctx.body = fs.createReadStream('views/log-out.html')
        } else {
            ctx.body = fs.createReadStream('views/log-in.html')
        }
    });

    // post update
    router.post('/', async(ctx, next) => {
        if (ctx.isAuthenticated()) {
            // Attach db to context
            ctx.langDB = db
            for (var keypath in ctx.request.body) {
                const content = ctx.request.body[keypath]
                // Frontend EOS has to convert . to / to avoid being broken up by bodyparser
                keypath = keypath.replace(/\//g, '.')
                ctx.langDB.set(keypath, content).write()
            }
            ctx.status = 200
        } else {
            ctx.status = 401
        }
    });

    // sample route
    router.get('/sample.js', async(ctx) => {
        ctx.type = 'application/json'
        if (ctx.isAuthenticated()) {
            ctx.body = fs.createReadStream('views/sample.js')
        } else {
            ctx.body = fs.createReadStream('views/sample-blank.js')
        }
    });

    // use routes
    app.use(router.routes()).use(router.allowedMethods());

    // serve public
    app.use(serve({
        rootDir: __dirname + '/public',
        rootPath: '/'
    }));
    return app
}

// export module
module.exports = () => {
    return appFactory()
}

the error I'm getting is

TypeError: Cannot read property 'store' of undefined
      at session (/Users/sysuser/Desktop/eos/node_modules/koa-session/index.js:41:13)
      at dispatch (/Users/sysuser/Desktop/eos/node_modules/koa-mount/node_modules/koa-compose/index.js:44:32)
      at /Users/sysuser/Desktop/eos/node_modules/koa-mount/node_modules/koa-compose/index.js:36:12
      at /Users/sysuser/Desktop/eos/node_modules/koa-mount/index.js:58:11
      at dispatch (/Users/sysuser/Desktop/eos/node_modules/koa-compose/index.js:42:32)
      at /Users/sysuser/Desktop/eos/node_modules/koa-compose/index.js:34:12
      at Server.handleRequest (/Users/sysuser/Desktop/eos/node_modules/koa/lib/application.js:136:14)
      at emitTwo (events.js:106:13)
      at Server.emit (events.js:194:7)
      at parserOnIncoming (_http_server.js:563:12)

I've tried using your package.json as a base - still the same. it doesn't matter what router module i use - it breaks at app.use(session({}, app)) - Any suggestions?

rkusa commented 6 years ago

I was on vacation, thus the later answer. Before digging into it, do you still have the error @htkoca ?

eddyw commented 6 years ago

@rkusa I'm having this exact same issue :/

ardeearam commented 2 years ago

I solved a similar issue with this. Turns out I need to do app.use(session(app)) on the topmost level, and not inside the mount.

On your code, this translates to putting .use(session(app)) inside server.js and NOT application.js