@travetto/auth-web-session
v7.1.4
Published
Web authentication session integration support for the Travetto framework
Maintainers
Readme
Web Auth Session
Web authentication session integration support for the Travetto framework
Install: @travetto/auth-web-session
npm install @travetto/auth-web-session
# or
yarn add @travetto/auth-web-sessionOne of Web Auth's main responsibility is being able to send, validate and receive authentication/authorization information from the client.
This module's main responsibilities is to expose Auth Session's data within the scope of an authenticated request flow.
Code: Anatomy of the Session Interceptor
export class AuthSessionInterceptor implements WebInterceptor {
category: WebInterceptorCategory = 'application';
dependsOn = [AuthContextInterceptor];
@Inject()
service: SessionService;
@Inject()
context: SessionContext;
@Inject()
webAsyncContext: WebAsyncContext;
postConstruct(): void {
this.webAsyncContext.registerSource(toConcrete<Session>(), () => this.context.get(true));
this.webAsyncContext.registerSource(toConcrete<SessionData>(), () => this.context.get(true).data);
}
async filter({ next }: WebChainedContext): Promise<WebResponse> {
try {
await this.service.load();
return await next();
} finally {
await this.service.persist();
}
}
}Once operating within the Session boundaries, the session state can be injected via @ContextParams, injected as SessionContext, or accessed via the SessionService.
Code: Sample Usage
@Authenticated()
@Controller('/session')
export class SessionEndpoints {
@Inject()
session: SessionContext;
@ContextParam()
data: SessionData;
@Put('/info')
async storeInfo() {
if (this.data) {
this.data.age = 20;
this.data.name = 'Roger'; // Setting data
}
}
@Get('/logout')
async logout() {
await this.session.destroy();
}
@Get('/info/age')
async getInfo() {
const { data } = this.session.get(true);
return data?.age;
}
}