JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q42636F
  • License MIT

Web authentication session integration support for the Travetto framework

Package Exports

  • @travetto/auth-web-session
  • @travetto/auth-web-session/__index__.ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@travetto/auth-web-session) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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-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

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;
  }
}