Loading backend/src/auth/user/authUsercontroller.ts +10 −12 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ import { import { AuthUserService } from './authUserservice'; import { CreateUserDto } from 'src/user/dto/create-user.dto'; import { LoginUserDto } from './dto/login-user.dto'; import { UserSigninResDto } from './dto/user-signin-res.dto'; @Controller('') @ApiTags('Create user account and sign in as user') Loading @@ -18,31 +19,28 @@ export class AuthUserController { @ApiBody({ type: CreateUserDto }) @ApiBearerAuth() @ApiCreatedResponse({ content: { 'application/json': { example: { token: 'token', }, }, }, type: UserSigninResDto, }) @Post('user/signup') async signUp(@Body() createAdminDto: CreateUserDto) { try { const accessToken = await this.authUserService.signUp(createAdminDto); return { token: accessToken }; const adminSigninResDto = await this.authUserService.signUp(createAdminDto); return { user: adminSigninResDto }; } catch (e) { throw e; } } @ApiBody({ type: LoginUserDto }) @ApiCreatedResponse({ type: UserSigninResDto, }) @ApiUnauthorizedResponse() @Post('user/signin') async signIn(@Body() loginAdminDto: LoginUserDto) { try { const adminSigninResDto = await this.authUserService.signIn(loginAdminDto); return adminSigninResDto; const userSigninResDto = await this.authUserService.signIn(loginAdminDto); return { user: userSigninResDto }; } catch (e) { throw e; } Loading backend/src/auth/user/authUserservice.ts +7 −6 Original line number Diff line number Diff line import { BadRequestException, HttpException, HttpStatus, Injectable, UnauthorizedException, } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { EncryptionService } from '../encryption/encryption.service'; Loading @@ -21,7 +20,7 @@ export class AuthUserService { private encryptionService: EncryptionService, ) {} async signUp(createAdminDto: CreateUserDto): Promise<string> { async signUp(createAdminDto: CreateUserDto): Promise<UserSigninResDto> { const loginAdminDto: LoginUserDto = { email: createAdminDto.email, password: createAdminDto.password, Loading @@ -30,30 +29,32 @@ export class AuthUserService { createAdminDto.password, ); createAdminDto.password = hashedPwd; if (this.userService.userExists(createAdminDto.email)) { if (await this.userService.userExists(createAdminDto.email)) { throw new BadRequestException('User already exists'); } await this.userService.create(createAdminDto); const adminSigninResDto: UserSigninResDto = await this.signIn(loginAdminDto); return adminSigninResDto.token; return adminSigninResDto; } async signIn(logInAdmin: LoginUserDto): Promise<UserSigninResDto> { const user: User = await this.userService.findOne(logInAdmin.email); if (!user) throw new UnauthorizedException('Invalid credentials'); const validPwd: boolean = await this.encryptionService.comparePassword( logInAdmin.password, user.password, ); if (!validPwd) { throw new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED); throw new UnauthorizedException('Invalid credentials'); } const accessToken = await this.jwtService.sign( { email: user.email, name: user.name, lastName: user.lastName }, { secret: JwtConstants.SECRET }, ); const userSigninResDto: UserSigninResDto = { userId: user.userId, email: user.email, name: user.name, lastName: user.lastName, Loading backend/src/auth/user/dto/user-signin-res.dto.ts +2 −0 Original line number Diff line number Diff line import { ApiProperty } from '@nestjs/swagger'; export class UserSigninResDto { @ApiProperty() userId: number; @ApiProperty() email: string; @ApiProperty() Loading backend/src/user/entities/user.entity.ts +1 −1 Original line number Diff line number Diff line Loading @@ -3,7 +3,7 @@ import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; userId: number; @Column({ unique: true, Loading backend/src/user/user.service.ts +1 −1 Original line number Diff line number Diff line Loading @@ -14,7 +14,7 @@ export class UserService { ) {} async findOne(email: string) { return await this.userRepository.findOneByOrFail({ email }); return await this.userRepository.findOneBy({ email }); } async create(createUserDto: CreateUserDto) { Loading Loading
backend/src/auth/user/authUsercontroller.ts +10 −12 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ import { import { AuthUserService } from './authUserservice'; import { CreateUserDto } from 'src/user/dto/create-user.dto'; import { LoginUserDto } from './dto/login-user.dto'; import { UserSigninResDto } from './dto/user-signin-res.dto'; @Controller('') @ApiTags('Create user account and sign in as user') Loading @@ -18,31 +19,28 @@ export class AuthUserController { @ApiBody({ type: CreateUserDto }) @ApiBearerAuth() @ApiCreatedResponse({ content: { 'application/json': { example: { token: 'token', }, }, }, type: UserSigninResDto, }) @Post('user/signup') async signUp(@Body() createAdminDto: CreateUserDto) { try { const accessToken = await this.authUserService.signUp(createAdminDto); return { token: accessToken }; const adminSigninResDto = await this.authUserService.signUp(createAdminDto); return { user: adminSigninResDto }; } catch (e) { throw e; } } @ApiBody({ type: LoginUserDto }) @ApiCreatedResponse({ type: UserSigninResDto, }) @ApiUnauthorizedResponse() @Post('user/signin') async signIn(@Body() loginAdminDto: LoginUserDto) { try { const adminSigninResDto = await this.authUserService.signIn(loginAdminDto); return adminSigninResDto; const userSigninResDto = await this.authUserService.signIn(loginAdminDto); return { user: userSigninResDto }; } catch (e) { throw e; } Loading
backend/src/auth/user/authUserservice.ts +7 −6 Original line number Diff line number Diff line import { BadRequestException, HttpException, HttpStatus, Injectable, UnauthorizedException, } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { EncryptionService } from '../encryption/encryption.service'; Loading @@ -21,7 +20,7 @@ export class AuthUserService { private encryptionService: EncryptionService, ) {} async signUp(createAdminDto: CreateUserDto): Promise<string> { async signUp(createAdminDto: CreateUserDto): Promise<UserSigninResDto> { const loginAdminDto: LoginUserDto = { email: createAdminDto.email, password: createAdminDto.password, Loading @@ -30,30 +29,32 @@ export class AuthUserService { createAdminDto.password, ); createAdminDto.password = hashedPwd; if (this.userService.userExists(createAdminDto.email)) { if (await this.userService.userExists(createAdminDto.email)) { throw new BadRequestException('User already exists'); } await this.userService.create(createAdminDto); const adminSigninResDto: UserSigninResDto = await this.signIn(loginAdminDto); return adminSigninResDto.token; return adminSigninResDto; } async signIn(logInAdmin: LoginUserDto): Promise<UserSigninResDto> { const user: User = await this.userService.findOne(logInAdmin.email); if (!user) throw new UnauthorizedException('Invalid credentials'); const validPwd: boolean = await this.encryptionService.comparePassword( logInAdmin.password, user.password, ); if (!validPwd) { throw new HttpException('Invalid credentials', HttpStatus.UNAUTHORIZED); throw new UnauthorizedException('Invalid credentials'); } const accessToken = await this.jwtService.sign( { email: user.email, name: user.name, lastName: user.lastName }, { secret: JwtConstants.SECRET }, ); const userSigninResDto: UserSigninResDto = { userId: user.userId, email: user.email, name: user.name, lastName: user.lastName, Loading
backend/src/auth/user/dto/user-signin-res.dto.ts +2 −0 Original line number Diff line number Diff line import { ApiProperty } from '@nestjs/swagger'; export class UserSigninResDto { @ApiProperty() userId: number; @ApiProperty() email: string; @ApiProperty() Loading
backend/src/user/entities/user.entity.ts +1 −1 Original line number Diff line number Diff line Loading @@ -3,7 +3,7 @@ import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id: number; userId: number; @Column({ unique: true, Loading
backend/src/user/user.service.ts +1 −1 Original line number Diff line number Diff line Loading @@ -14,7 +14,7 @@ export class UserService { ) {} async findOne(email: string) { return await this.userRepository.findOneByOrFail({ email }); return await this.userRepository.findOneBy({ email }); } async create(createUserDto: CreateUserDto) { Loading