npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flowcore/nestjs-clerk-guard

v1.0.2

Published

NestJS Library for protecting services using Clerk.com Authentication

Downloads

4

Readme

Build

Nestjs Clerk-guard

NestJS Library for protecting services using Clerk.com Authentication

Installation

install with npm:

npm install @flowcore/nestjs-clerk-guard @flowcore/microservice

or yarn:

yarn add @flowcore/nestjs-clerk-guard @flowcore/microservice

If you are using GraphQL, you also need to install the @nestjs/graphql package.

Configuration

Import the ClerkGuardModule into your NestJS application and configure it with your Keycloak server's details:

import { ClerkGuardModuleBuilder, ClerkGuardConfigurationSchema } from '@flowcore/nestjs-clerk-guard';
import { ConfigModule, ConfigFactory } from '@flowcore/microservice';
import { AuthGuardBuilder } from "./auth-guard.builder";

const config = ConfigModule.forRoot(
  new ConfigFactory()
    // ...other configuration schemas
    .withSchema(ClerkGuardConfigurationSchema)
  // ...other configuration schemas,
);

@Module({
  imports: [
    config,
    // ...other modules
    new ClerkGuardModuleBuilder().withConfig(config).build(),
  ],
  providers: [
    ...new AuthGuardBuilder()
      .usingClaimGuard()
      .build(),
  ],
})
export class AppModule {
}

This can then be configured in a service with the following environment variables

| Environment Variable | Description | Type | Default Value | Required | |----------------------|------------------------------------------------------------------------------------------------|:--------:|---------------|:--------:| | CLERK_JWKS_URL | The URL to the JWKS endpoint for your Clerk.com application. | string | | ✅ |

Usage

The AuthGuard is global and will protect all routes by default. You can use the @Public() decorator to exclude routes from the AuthGuard.

import { Public } from '@flowcore/nestjs-clerk-guard';
import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  @Public()
  getHello(): string {
    return 'Hello World!';
  }
}

You can also use the @ClaimsRequired() decorators to protect routes with a ClaimGuard. The @ClaimsRequired() decorator accepts a list of claims. If the user has one of the claims in the token, the route will be accessible.

import { Roles } from '@flowcore/nestjs-clerk-guard';
import { Controller, Get } from '@nestjs/common';
import { ClaimsRequired } from "./claims.decorator";

@Controller()
export class AppController {
  @ClaimsRequired(['some_key'])
  getHello(): string {
    return 'Hello World!';
  }
}

For public endpoints like /health or /metrics you can use the built-in public endpoints are used to keep these paths open. If you want to override these defaults or disable them, you can do so by configuring the ClerkGuardModule using the ClerkGuardModuleBuilder:

@Module({
  imports: [
    config,
    // ...other modules
    new ClerkGuardModuleBuilder()
      .withConfig(config)
      .noPublicEndpoints() // for disabling the built-in public endpoints
      .overridePublicEndpoints(
        [
          '/health',
          '/metrics',
          '/healthz',
        ],
      )
      .build(),
  ],
  // ... other provider, controllers etc.
})
export class AppModule {
}

To get access to the user information in the request, you can use the @AuthenticatedUser() decorator:

import { AuthenticatedUser } from '@flowcore/nestjs-clerk-guard';
import { Controller, Get } from '@nestjs/common';

@Controller()
export class AppController {
  @Get()
  getHello(@AuthenticatedUser() user: any /* change to match your token */): string {
    return 'Hello World!';
  }
}

This also works with @Resolvers() in GraphQL.

We hope you find this library useful in your NestJS projects!

Development

yarn install

or with npm:

npm install