Web authentication session integration support for the Travetto framework
npm install @travetto/auth-web-sessionInstall: @travetto/auth-web-session
``bash
npm install @travetto/auth-web-session
yarn add @travetto/auth-web-session
`
One 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
`typescript
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
this.webAsyncContext.registerSource(toConcrete
}
async filter({ next }: WebChainedContext): Promise
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
`typescript
@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;
}
}
``