From 23191756a2af296659e3e67770e7ffc8fb91d662 Mon Sep 17 00:00:00 2001 From: Tomasz Wysocki <121371814+tomaszwysocki@users.noreply.github.com> Date: Fri, 23 Feb 2024 23:28:32 +0100 Subject: [PATCH 1/2] Add type definitions and fix arrow function param I modified the User type to include the appropriate values. Additionally, I enclosed a sole arrow function parameter in parentheses, following the same convention enforced by Nest's Prettier. --- content/security/authentication.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/content/security/authentication.md b/content/security/authentication.md index 2c19d246dc..ab0a27eb47 100644 --- a/content/security/authentication.md +++ b/content/security/authentication.md @@ -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 { @@ -48,7 +51,7 @@ export class UsersService { ]; async findOne(username: string): Promise { - return this.users.find(user => user.username === username); + return this.users.find((user) => user.username === username); } } @@switch @@ -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); } } ``` @@ -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 'src/users/users.service'; import { AuthService } from './auth.service'; @Controller('auth') @@ -192,13 +196,13 @@ export class AuthController { @HttpCode(HttpStatus.OK) @Post('login') - signIn(@Body() signInDto: Record) { + signIn(@Body() signInDto: Pick) { return this.authService.signIn(signInDto.username, signInDto.password); } } ``` -> info **Hint** Ideally, instead of using the `Record` 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` type, we should use a DTO class to define the shape of the request body. See the [validation](/techniques/validation) chapter for more information. From 5459a00b31c19855f0cf352682abeff501a36153 Mon Sep 17 00:00:00 2001 From: Tomasz Wysocki <121371814+tomaszwysocki@users.noreply.github.com> Date: Thu, 29 Feb 2024 17:48:17 +0100 Subject: [PATCH 2/2] fix: apply suggestions from code review Co-authored-by: Micael Levi L. Cavalcante --- content/security/authentication.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/security/authentication.md b/content/security/authentication.md index ab0a27eb47..8ff2df290c 100644 --- a/content/security/authentication.md +++ b/content/security/authentication.md @@ -187,7 +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 'src/users/users.service'; +import { User } from '../users/users.service'; import { AuthService } from './auth.service'; @Controller('auth')