lib/src/http/kernel/module/InjectSessionHTTPModule.ts

33 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-03-07 19:26:14 +00:00
import {HTTPKernelModule} from "../HTTPKernelModule";
import {Injectable} from "@extollo/di"
import {ErrorWithContext} from "@extollo/util"
import {HTTPKernel} from "../HTTPKernel";
import {Request} from "../../lifecycle/Request";
import {SetSessionCookieHTTPModule} from "./SetSessionCookieHTTPModule";
import {SessionFactory} from "../../session/SessionFactory";
import {Session} from "../../session/Session";
@Injectable()
export class InjectSessionHTTPModule extends HTTPKernelModule {
2021-03-09 15:42:19 +00:00
public readonly executeWithBlockingWriteback = true
2021-03-07 19:26:14 +00:00
public static register(kernel: HTTPKernel) {
kernel.register(this).after(SetSessionCookieHTTPModule)
}
public async apply(request: Request) {
request.registerFactory(new 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
}
}