Skip to content

Commit 5e57ba5

Browse files
docs(): add authorization chapter
1 parent 9a9ea49 commit 5e57ba5

File tree

6 files changed

+464
-19
lines changed

6 files changed

+464
-19
lines changed

content/guards.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export class AuthGuard {
4040
}
4141
```
4242

43+
> info **Hint** If you are looking for a real-world example on how to implement an authentication mechanism in your application, visit [this chapter](/security/authentication). Likewise, for more sophisticated authorization example, check [this page](/security/authorization).
44+
4345
The logic inside the `validateRequest()` function can be as simple or sophisticated as needed. The main point of this example is to show how guards fit into the request/response cycle.
4446

4547
Every guard must implement a `canActivate()` function. This function should return a boolean, indicating whether the current request is allowed or not. It can return the response either synchronously or asynchronously (via a `Promise` or `Observable`). Nest uses the return value to control the next action:
@@ -241,7 +243,7 @@ export class RolesGuard {
241243
}
242244
```
243245

244-
> info **Hint** In the node.js world, it's common practice to attach the authorized user to the `request` object. Thus, in our sample code above, we are assuming that `request.user` contains the user instance and allowed roles. In your app, you will probably make that association in your custom **authentication guard** (or middleware).
246+
> info **Hint** In the node.js world, it's common practice to attach the authorized user to the `request` object. Thus, in our sample code above, we are assuming that `request.user` contains the user instance and allowed roles. In your app, you will probably make that association in your custom **authentication guard** (or middleware). Check [this chapter](/security/authentication) for more infomration on this topic.
245247
246248
> warning **Warning** The logic inside the `matchRoles()` function can be as simple or sophisticated as needed. The main point of this example is to show how guards fit into the request/response cycle.
247249
@@ -264,3 +266,5 @@ throw new UnauthorizedException();
264266
```
265267

266268
Any exception thrown by a guard will be handled by the [exceptions layer](/exception-filters) (global exceptions filter and any exceptions filters that are applied to the current context).
269+
270+
> info **Hint** If you are looking for a real-world example on how to implement authorization, check [this chapter](/security/authorization).

content/security/authentication.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,18 @@ export type User = any;
6363

6464
@Injectable()
6565
export class UsersService {
66-
private readonly users: User[];
67-
68-
constructor() {
69-
this.users = [
70-
{
71-
userId: 1,
72-
username: 'john',
73-
password: 'changeme',
74-
},
75-
{
76-
userId: 2,
77-
username: 'maria',
78-
password: 'guess',
79-
},
80-
];
81-
}
66+
private readonly users = [
67+
{
68+
userId: 1,
69+
username: 'john',
70+
password: 'changeme',
71+
},
72+
{
73+
userId: 2,
74+
username: 'maria',
75+
password: 'guess',
76+
},
77+
];
8278

8379
async findOne(username: string): Promise<User | undefined> {
8480
return this.users.find(user => user.username === username);
@@ -836,7 +832,7 @@ export const IS_PUBLIC_KEY = 'isPublic';
836832
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true);
837833
```
838834

839-
In the file above, we exported two constants. One being our metadata key named `IS_PUBLIC_KEY`, and the other being our new decorator itself that we’re going to call `Public` (we can alternatively name it `SkipAuth`).
835+
In the file above, we exported two constants. One being our metadata key named `IS_PUBLIC_KEY`, and the other being our new decorator itself that we’re going to call `Public` (you can alternatively name it `SkipAuth` or `AllowAnon`, whatever fits your project).
840836

841837
Now that we have a custom `@Public()` decorator, we can use it to decorate any method, as follows:
842838

@@ -853,7 +849,9 @@ Lastly, we need the `JwtAuthGuard` to return `true` when the `"isPublic"` metada
853849
```typescript
854850
@Injectable()
855851
export class JwtAuthGuard extends AuthGuard('jwt') {
856-
constructor(private reflector: Reflector) {}
852+
constructor(private reflector: Reflector) {
853+
super();
854+
}
857855

858856
canActivate(context: ExecutionContext) {
859857
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [

0 commit comments

Comments
 (0)