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

33 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-06-03 03:36:25 +00:00
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'
2021-03-25 13:50:13 +00:00
/**
* 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 {
2021-03-09 15:42:19 +00:00
public readonly executeWithBlockingWriteback = true
@Inject()
protected readonly logging!: Logging
2021-06-03 03:36:25 +00:00
public static register(kernel: HTTPKernel): void {
2021-03-07 19:26:14 +00:00
kernel.register(this).first()
}
2021-06-03 03:36:25 +00:00
public async apply(request: Request): Promise<Request> {
if ( !request.cookies.has('extollo.session') ) {
2021-06-03 03:36:25 +00:00
const session = `${uuid4()}-${uuid4()}`
this.logging.verbose(`Starting session: ${session}`)
request.cookies.set('extollo.session', session)
}
return request
}
}