You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/http/kernel/module/InjectSessionHTTPModule.ts

37 lines
1.2 KiB

import {HTTPKernelModule} from '../HTTPKernelModule'
import {Injectable} from '../../../di'
import {ErrorWithContext} from '../../../util'
import {HTTPKernel} from '../HTTPKernel'
import {Request} from '../../lifecycle/Request'
import {SetSessionCookieHTTPModule} from './SetSessionCookieHTTPModule'
import {SessionFactory} from '../../session/SessionFactory'
import {Session} from '../../session/Session'
/**
* HTTP kernel middleware that creates the session using the configured driver
* and loads its data using the request's session cookie.
*/
@Injectable()
export class InjectSessionHTTPModule extends HTTPKernelModule {
public readonly executeWithBlockingWriteback = true
public static register(kernel: HTTPKernel): void {
kernel.register(this).after(SetSessionCookieHTTPModule)
}
public async apply(request: Request): Promise<Request> {
request.registerFactory(request.make(SessionFactory))
const session = <Session> request.make(Session)
const id = request.cookies.get('extollo.session')
if ( !id ) {
throw new ErrorWithContext('Session ID has not been set. Cannot inject session!')
}
session.setKey(id.value)
await session.load()
return request
}
}