@getlarge/kratos-client-wrapper
TypeScript icon, indicating that this package has built-in type declarations

0.1.8 • Public • Published

kratos-client-wrapper

npm

This library is a wrapper around the Ory Kratos client - @ory/client. It provides :

  • OryIdentitiesModule: a module to interact with the Ory Kratos Identities API
  • OryFrontendModule: a module to interact with the Ory Kratos Frontend API
  • OryAuthenticationGuard: a guard to protect your routes based on the Ory Kratos session

Install

npm install @getlarge/kratos-client-wrapper

Usage

Import the module in your app:

import {
  OryIdentitiesModule,
  OryFrontendModule,
} from '@getlarge/kratos-client-wrapper';
import { Module } from '@nestjs/common';

@Module({
  imports: [
    OryIdentitiesModule.forRoot({
      basePath: 'http://localhost:4434',
      accessToken: 'my-access-token',
    }),
    OryFrontendModule.forRootAsync({
      useFactory: () => ({
        baseUrl: 'http://localhost:4433',
      }),
    }),
  ],
})
export class YourModule {}

Inject the service in your provider:

import { OryIdentitiesService } from '@getlarge/kratos-client-wrapper';
import { Injectable } from '@nestjs/common';

@Injectable()
export class YourService {
  constructor(private readonly oryIdentitiesService: OryIdentitiesService) {}
}

Use the Guard to protect your routes:

import { OryAuthenticationGuard } from '@getlarge/kratos-client-wrapper';
import { Controller, Get, Logger, UseGuards } from '@nestjs/common';

@Controller()
export class YourController {
  @UseGuards(
    OryAuthenticationGuard({
      postValidationHook: (ctx, session) => {
        const req = ctx.switchToHttp().getRequest();
        req.session = session;
        req.user = session.identity;
      },
      isValidSession(session): boolean {
        return !!session.active;
      },
      sessionTokenResolver: (ctx) =>
        ctx
          .switchToHttp()
          .getRequest()
          ?.headers?.authorization?.replace('Bearer ', ''),
    })
  )
  @Get()
  async get() {
    return 'Hello World';
  }
}

Development

Building

Run nx build kratos-client-wrapper to build the library.

Running unit tests

Run nx test kratos-client-wrapper to execute the unit tests via Jest.

Readme

Keywords

none

Package Sidebar

Install

npm i @getlarge/kratos-client-wrapper

Weekly Downloads

67

Version

0.1.8

License

MIT

Unpacked Size

61.1 kB

Total Files

51

Last publish

Collaborators

  • getlarge