31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
|
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 {
|
||
|
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
|
||
|
}
|
||
|
}
|