Cannot read property 'get' of undefined using session auth with ajs 4.1


#1

I’ve installed the auto module, selected session + lucid, but now I get the following error on all routes:

Cannot read property 'get' of undefined

Which originates from node_modules/@adonisjs/auth/src/Schemes/Session.js:356:

const sessionValue = this._ctx.session.get(this.sessionKey)

I’ve set up the proper providers in app.js and kernel.js as specified in the docs. I managed to get basic auth to work this way, but with session auth I can’t get passed this error.

Am I overlooking something obvious or is this an adonis problem?


#2

Can you share start/kernel.js file ?


#3

Certainly:

'use strict'

const Server = use('Server')

/*
|--------------------------------------------------------------------------
| Global Middleware
|--------------------------------------------------------------------------
|
| Global middleware are executed on each http request only when the routes
| match.
|
*/
const globalMiddleware = [
    'Adonis/Middleware/BodyParser',
    'Adonis/Middleware/AuthInit'
]

/*
|--------------------------------------------------------------------------
| Named Middleware
|--------------------------------------------------------------------------
|
| Named middleware is key/value object to conditionally add middleware on
| specific routes or group of routes.
|
| // define
| {
|   auth: 'Adonis/Middleware/Auth'
| }
|
| // use
| Route.get().middleware('auth')
|
*/
const namedMiddleware = {
    auth: 'Adonis/Middleware/Auth'
}

Server
  .registerGlobal(globalMiddleware)
  .registerNamed(namedMiddleware)
  .use(['Adonis/Middleware/Static'])

It’s the default kernel.js but with the config changes from the documentation.


#4

Doesn’t seems to be like the default file https://github.com/adonisjs/adonis-fullstack-app/blob/develop/start/kernel.js.

You are missing the Adonis/Middleware/Session middleware.


#5

That’s the default for a full stack install, which I’m not using. I’m following the setup instructions from the documentation page I previously linked to. It gives instructions for non-full stack installs, but It doesn’t say anything about specific session middleware on that page.

I’ve added ‘Adonis/Middleware/Session’ to the global middleware, and ‘’@adonisjs/session/providers/SessionProvider’ to the providers list, since that also seems to be required. After that, I get the following error:

Error: Cannot find module '@adonisjs/session/providers/SessionProvider'

This was resolved with a lucky guess:

adonis install @adonisjs/session

I got another error after that, which seemed to be resolved by moving the Session middleware reference to before the the AuthInit middleware reference.

The pages now get served again, but I can’t do session testing until later. I feel the documentation is lacking here.


#6

I believe it’s all about common sense.

All docs cannot give setup instructions based upon what’s the current situation of your app.

What you call a lucky guess is again common sense. That if you are trying to rely on sessions, then you need to install the dependencies related to that and most probably read the docs around session.

So please do not blame docs or the errors, if you itself not ready to read the errors and docs.


#7

Common sense does not magically give you information you didn’t have before: the docs are incomplete. I’m not sure why you’re taking this personally.

The issue is resolved now, thanks for trying to help.