daton/lib/src/http/kernel/module/SetSessionCookie.ts

29 lines
817 B
TypeScript
Raw Normal View History

2020-07-22 14:38:17 +00:00
import Module from '../Module.ts'
import Kernel from '../Kernel.ts'
import {Request} from '../../Request.ts'
import PrepareRequest from './PrepareRequest.ts'
import Utility from '../../../service/utility/Utility.ts'
import {Injectable} from '../../../../../di/src/decorator/Injection.ts'
@Injectable()
export default class SetSessionCookie extends Module {
public static register(kernel: Kernel) {
kernel.register(this).after(PrepareRequest)
}
constructor(
protected utility: Utility,
) {
super()
}
public async apply(request: Request): Promise<Request> {
if ( !(await request.cookies.has('daton.session')) ) {
await request.cookies.set('daton.session', `${this.utility.uuid()}-${this.utility.uuid()}`)
}
return request
}
}