Skip to content

Commit

Permalink
fix access token decorator, add user decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Michelle Laurenti committed Feb 26, 2024
1 parent b2f5320 commit 88c41e5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
51 changes: 30 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,33 +225,15 @@ using the `@Context("req")` and `@Context("res")` decorators.
## Getting the tokens inside a controller or resolver
You can access the parsed access token and refresh token
You can access the access token and refresh token
inside your controllers and resolvers using decorators.
```typescript
import {
Authenticated,
AccessToken,
HasPermission,
} from "@moveaxlab/nestjs-security";

interface User {
tokenType: "admin" | "user";
uid: string;
permission: string[];
// other information contained in the token
}
import { Authenticated, AccessToken } from "@moveaxlab/nestjs-security";

@Authenticated("admin")
class MyController {
async myMethod(@AccessToken() token: User) {
// use the token here
}
}

@HasPermission("myPermission")
class MySecondController {
async mySecondMethod(@AccessToken() token: User) {
async myMethod(@AccessToken() token: string) {
// use the token here
}
}
Expand All @@ -276,6 +258,33 @@ class MyController {
}
```

You can access the parsed access token using the `@User` decorator.

```typescript
import { Authenticated, HasPermission, User } from "@moveaxlab/nestjs-security";

interface UserType {
tokenType: "admin" | "user";
uid: string;
permission: string[];
// other information contained in the token
}

@Authenticated("admin")
class MyController {
async myMethod(@User() token: UserType) {
// use the token here
}
}

@HasPermission("myPermission")
class MySecondController {
async mySecondMethod(@User() token: UserType) {
// use the token here
}
}
```

## Using different secrets based on the issuer

The `jwtSecret` options can accept an object mapping the `iss` key
Expand Down
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { Authenticated } from "./authenticated/authenticated.decorator";
export { HasPermission } from "./has-permission/has-permission.decorator";
export { RefreshCookieInterceptor } from "./refresh-cookie.interceptor";
export { RefreshToken } from "./refresh-token.decorator";
export { User } from "./user.decorator";
9 changes: 9 additions & 0 deletions src/decorators/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createParamDecorator, ExecutionContext } from "@nestjs/common";
import { getRequest } from "../utils";

/**
* Injects the parsed access token for the current request.
*/
export const User = createParamDecorator((_, context: ExecutionContext) => {
return getRequest(context).user;
});

0 comments on commit 88c41e5

Please sign in to comment.