@getlarge/kratos-client-wrapper
v0.2.2
Published
NestJS Module wrapper for the Ory Kratos API
Downloads
683
Readme
kratos-client-wrapper
This library is a wrapper around the Ory Kratos client - @ory/client. It provides :
OryIdentitiesModule
: a module to interact with the Ory Kratos Identities APIOryFrontendModule
: a module to interact with the Ory Kratos Frontend APIOryAuthenticationGuard
: 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.