Add basic memory-based session driver
This commit is contained in:
30
src/http/kernel/module/InjectSessionHTTPModule.ts
Normal file
30
src/http/kernel/module/InjectSessionHTTPModule.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
18
src/http/kernel/module/PersistSessionHTTPMiddleware.ts
Normal file
18
src/http/kernel/module/PersistSessionHTTPMiddleware.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import {HTTPKernelModule} from "../HTTPKernelModule";
|
||||
import {Injectable} from "@extollo/di"
|
||||
import {HTTPKernel} from "../HTTPKernel";
|
||||
import {Request} from "../../lifecycle/Request";
|
||||
import {Session} from "../../session/Session";
|
||||
|
||||
@Injectable()
|
||||
export class PersistSessionHTTPMiddleware extends HTTPKernelModule {
|
||||
public static register(kernel: HTTPKernel) {
|
||||
kernel.register(this).last()
|
||||
}
|
||||
|
||||
public async apply(request: Request): Promise<Request> {
|
||||
const session = <Session> request.make(Session)
|
||||
await session.persist()
|
||||
return request
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ export class SetSessionCookieHTTPModule extends HTTPKernelModule {
|
||||
protected readonly logging!: Logging
|
||||
|
||||
public static register(kernel: HTTPKernel) {
|
||||
kernel.register(this).first() // TODO make this before inject session?
|
||||
kernel.register(this).first()
|
||||
}
|
||||
|
||||
public async apply(request: Request) {
|
||||
|
||||
Reference in New Issue
Block a user