feat: add jwt auth guard and profile endpoint
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
import { MatchField } from '@/common/decorators/match-field.decorator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import {
|
||||
IsOptional,
|
||||
IsString,
|
||||
Matches,
|
||||
MaxLength,
|
||||
MinLength,
|
||||
ValidateIf,
|
||||
} from 'class-validator';
|
||||
import { i18nValidationMessage as t } from 'nestjs-i18n';
|
||||
|
||||
export class UpdateProfileDTO {
|
||||
@ApiProperty({
|
||||
type: 'string',
|
||||
description: `User's Firstname`,
|
||||
example: 'Radmehr',
|
||||
required: false,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@MinLength(3, {
|
||||
message: t('validation.minLengthField', {
|
||||
field: '$t(users.fields.firstName)',
|
||||
min: 3,
|
||||
}),
|
||||
})
|
||||
@MaxLength(96, {
|
||||
message: t('validation.maxLengthField', {
|
||||
field: '$t(users.fields.firstName)',
|
||||
max: 96,
|
||||
}),
|
||||
})
|
||||
firstName?: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: 'string',
|
||||
description: `User's Lastname`,
|
||||
example: 'Tarnas',
|
||||
required: false,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@MinLength(3, {
|
||||
message: t('validation.minLengthField', {
|
||||
field: '$t(users.fields.lastName)',
|
||||
min: 3,
|
||||
}),
|
||||
})
|
||||
@MaxLength(96, {
|
||||
message: t('validation.maxLengthField', {
|
||||
field: '$t(users.fields.lastName)',
|
||||
min: 3,
|
||||
}),
|
||||
})
|
||||
lastName?: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: 'string',
|
||||
description: 'Password for User Account',
|
||||
example: 'Password1@',
|
||||
required: false,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@MinLength(8, {
|
||||
message: t('validation.minLengthField', {
|
||||
field: '$t(users.fields.password)',
|
||||
min: 8,
|
||||
}),
|
||||
})
|
||||
@MaxLength(96, {
|
||||
message: t('validation.maxLengthField', {
|
||||
field: '$t(users.fields.password)',
|
||||
max: 96,
|
||||
}),
|
||||
})
|
||||
@Matches(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[^A-Za-z\d]).{8,}$/, {
|
||||
message: t('users.errors.passwordPattern'),
|
||||
})
|
||||
password?: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: 'string',
|
||||
description: 'Confirm Password of User Account (Should match Password)',
|
||||
example: 'Password1@',
|
||||
required: false,
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
@ValidateIf((o) => o.password !== undefined)
|
||||
@IsOptional()
|
||||
@MatchField('password')
|
||||
confirmPassword?: string;
|
||||
}
|
||||
@@ -1,3 +1,2 @@
|
||||
GET http://localhost:3000/users
|
||||
|
||||
GET http://localhost:3000/users/2
|
||||
GET http://localhost:3000/users/profile
|
||||
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjQsInBob25lIjoiMDkxMjYxMTcwMTgiLCJpYXQiOjE3NzIxMjc1MTgsImV4cCI6MTc3MjEzMTExOCwiYXVkIjoibG9jYWxob3N0IiwiaXNzIjoibG9jYWxob3N0In0.oaxzSd0DDakCDMiW129Ou3MJh0oT0zJTFerZEjnQ7Ug
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
PATCH http://localhost:3000/users/profile
|
||||
Content-Type: application/json
|
||||
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjUsInBob25lIjoiMDkxMjExMTExMTEiLCJpYXQiOjE3NzIxNzQwMzUsImV4cCI6MTc3MjE3NzYzNSwiYXVkIjoibG9jYWxob3N0IiwiaXNzIjoibG9jYWxob3N0In0.bux4kC-J9AV8HJpo41t8-iRGDJwVOVb0yMK9cAs4ELA
|
||||
|
||||
{
|
||||
"firstName": "Radmehr",
|
||||
"lastName": "Tarnas",
|
||||
"password": "Password1@",
|
||||
"confirmPassword": "ldsfjlsdf"
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
POST http://localhost:3000/users
|
||||
Content-Type: application/json
|
||||
lang: fa
|
||||
|
||||
{
|
||||
"firstName": "Ra",
|
||||
"lastName": "Tnas",
|
||||
"phone": "9333026363",
|
||||
"password": "ra"
|
||||
}
|
||||
@@ -38,4 +38,13 @@ export class UsersService {
|
||||
|
||||
return newUser;
|
||||
}
|
||||
|
||||
public getProfile(user: User) {
|
||||
return {
|
||||
phone: user.phone,
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
dateJoined: user.createdAt,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
import { Body, Controller, Get, Patch } from '@nestjs/common';
|
||||
import { UsersService } from './providers/users.service';
|
||||
import { ActiveUser } from '@/common/decorators/active-user.decorator';
|
||||
import { User } from './user.entity';
|
||||
import { UpdateProfileDTO } from './dto/update-profile.dto';
|
||||
|
||||
@Controller('users')
|
||||
export class UsersController {
|
||||
@@ -10,18 +13,16 @@ export class UsersController {
|
||||
private readonly usersService: UsersService,
|
||||
) {}
|
||||
|
||||
// @Get()
|
||||
// public getAllUsers() {
|
||||
// return ['user1', 'user2'];
|
||||
// }
|
||||
@Get('profile')
|
||||
public getUserProfile(@ActiveUser() user: User) {
|
||||
return this.usersService.getProfile(user);
|
||||
}
|
||||
|
||||
// @Get(':id')
|
||||
// public getUserById(@Param('id', ParseIntPipe) id: number) {
|
||||
// return `user ${id}`;
|
||||
// }
|
||||
|
||||
// @Post()
|
||||
// public async createUser(@Body() createUserDto: CreateUserDTO) {
|
||||
// return await this.usersService.createOne(createUserDto);
|
||||
// }
|
||||
@Patch('profile')
|
||||
public updateUserProfile(
|
||||
@ActiveUser() user: User,
|
||||
@Body() updateProfileDto: UpdateProfileDTO,
|
||||
) {
|
||||
return updateProfileDto;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user