Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add type definitions and fix arrow function param #2979

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions content/security/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ Replace the default contents of these generated files as shown below. For our sa
@@filename(users/users.service)
import { Injectable } from '@nestjs/common';

// This should be a real class/interface representing a user entity
export type User = any;
export interface User {
userId: number;
username: string;
password: string;
}

@Injectable()
export class UsersService {
Expand All @@ -48,7 +51,7 @@ export class UsersService {
];

async findOne(username: string): Promise<User | undefined> {
return this.users.find(user => user.username === username);
return this.users.find((user) => user.username === username);
}
}
@@switch
Expand All @@ -72,7 +75,7 @@ export class UsersService {
}

async findOne(username) {
return this.users.find(user => user.username === username);
return this.users.find((user) => user.username === username);
}
}
```
Expand Down Expand Up @@ -184,6 +187,7 @@ With this in place, let's open up the `AuthController` and add a `signIn()` meth
```typescript
@@filename(auth/auth.controller)
import { Body, Controller, Post, HttpCode, HttpStatus } from '@nestjs/common';
import { User } from '../users/users.service';
import { AuthService } from './auth.service';

@Controller('auth')
Expand All @@ -192,13 +196,13 @@ export class AuthController {

@HttpCode(HttpStatus.OK)
@Post('login')
signIn(@Body() signInDto: Record<string, any>) {
signIn(@Body() signInDto: Pick<User, 'username' | 'password'>) {
return this.authService.signIn(signInDto.username, signInDto.password);
}
}
```

> info **Hint** Ideally, instead of using the `Record<string, any>` type, we should use a DTO class to define the shape of the request body. See the [validation](/techniques/validation) chapter for more information.
> info **Hint** Ideally, instead of using the `Pick<User, 'username' | 'password'>` type, we should use a DTO class to define the shape of the request body. See the [validation](/techniques/validation) chapter for more information.

<app-banner-courses-auth></app-banner-courses-auth>

Expand Down