lib/src/http/kernel/module/SetSessionCookieHTTPModule.ts
garrettmills 1d5056b753
All checks were successful
continuous-integration/drone/push Build is passing
Setup eslint and enforce rules
2021-06-02 22:36:25 -05:00

33 lines
1.0 KiB
TypeScript

import {HTTPKernelModule} from '../HTTPKernelModule'
import {Injectable, Inject} from '../../../di'
import {uuid4} from '../../../util'
import {HTTPKernel} from '../HTTPKernel'
import {Request} from '../../lifecycle/Request'
import {Logging} from '../../../service/Logging'
/**
* HTTP kernel middleware that tries to look up the session ID from the request.
* If none exists, generates a new one and sets the cookie.
*/
@Injectable()
export class SetSessionCookieHTTPModule extends HTTPKernelModule {
public readonly executeWithBlockingWriteback = true
@Inject()
protected readonly logging!: Logging
public static register(kernel: HTTPKernel): void {
kernel.register(this).first()
}
public async apply(request: Request): Promise<Request> {
if ( !request.cookies.has('extollo.session') ) {
const session = `${uuid4()}-${uuid4()}`
this.logging.verbose(`Starting session: ${session}`)
request.cookies.set('extollo.session', session)
}
return request
}
}