Skip to content

Commit

Permalink
fix(nestjs-clerk): use claims guard and tokenize decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
y-nk committed Jan 19, 2025
1 parent 3785045 commit 69f8c2d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nestjs-clerk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@y_nk/nestjs-clerk",
"version": "1.0.2",
"version": "1.0.3",
"description": "auth and permission guards using clerk for nestjs",
"homepage": "https://github.com/y-nk/nonorepo/tree/main/astro-components",
"keywords": [
Expand Down
4 changes: 3 additions & 1 deletion nestjs-clerk/src/acl.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import {
} from "@nestjs/common";
import { Reflector } from "@nestjs/core";

import { CLERK_ACL } from "./tokens";

@Injectable()
export class ClerkAclGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(context: ExecutionContext) {
if (context.getType() !== "http") return true;

const permission = this.reflector.get<string | undefined>(
"ClerkAcl",
CLERK_ACL,
context.getHandler(),
);

Expand Down
4 changes: 3 additions & 1 deletion nestjs-clerk/src/claim.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
type ExecutionContext
} from "@nestjs/common";
import { Reflector } from "@nestjs/core";

import { CLERK_CLAIMS } from "./tokens";
import { get } from "./utils";

type ClerkClaims = Record<string, string | number | boolean>
Expand All @@ -17,7 +19,7 @@ export class ClerkClaimsGuard implements CanActivate {
if (context.getType() !== "http") return true;

const claims = this.reflector.get<ClerkClaims | undefined>(
"ClerkClaims",
CLERK_CLAIMS,
context.getHandler(),
);

Expand Down
7 changes: 4 additions & 3 deletions nestjs-clerk/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import {
type CustomDecorator,
type ExecutionContext,
} from "@nestjs/common";
import { CLERK_ACL, CLERK_CLAIMS, NO_CLERK_AUTH } from "./tokens";

export function NoAuth(): CustomDecorator {
return SetMetadata("NoClerkAuth", true);
return SetMetadata(NO_CLERK_AUTH, true);
}

export function ClerkAcl(permission: string): CustomDecorator {
return SetMetadata("ClerkAcl", permission);
return SetMetadata(CLERK_ACL, permission);
}

export function ClerkClaims(claims: Record<string, string | number | boolean>): CustomDecorator {
return SetMetadata("ClerkClaims", claims);
return SetMetadata(CLERK_CLAIMS, claims);
}

export const Clerk = createParamDecorator(
Expand Down
6 changes: 6 additions & 0 deletions nestjs-clerk/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { APP_GUARD } from "@nestjs/core";

import { ClerkAclGuard } from "./acl.guard";
import { ClerkAuthGuard } from "./auth.guard";
import { ClerkClaimsGuard } from "./claim.guard";
import { CLERK_CLIENT, CLERK_CONFIG } from "./tokens";
import type { ClerkConfig } from "./types";

const providers: Provider[] = [
ClerkAuthGuard,
ClerkAclGuard,
ClerkClaimsGuard,
{
provide: APP_GUARD,
useExisting: ClerkAuthGuard,
Expand All @@ -18,6 +20,10 @@ const providers: Provider[] = [
provide: APP_GUARD,
useExisting: ClerkAclGuard,
},
{
provide: APP_GUARD,
useExisting: ClerkClaimsGuard,
},
{
provide: CLERK_CLIENT,
useValue: clerkClient,
Expand Down
6 changes: 6 additions & 0 deletions nestjs-clerk/src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export const CLERK_CONFIG = "CLERK_CONFIG";
export const CLERK_CLIENT = "CLERK_CLIENT";

export const NO_CLERK_AUTH = "NoClerkAuth";

export const CLERK_ACL = "ClerkAcl";

export const CLERK_CLAIMS = "ClerkClaims";

0 comments on commit 69f8c2d

Please sign in to comment.