diff --git a/backend/src/auth/user/authUsercontroller.ts b/backend/src/auth/user/authUsercontroller.ts index dd4bd9aaf989ace1ce269e4df45c4efed3196ed0..bc072bbf725ffbf758d5a86dcf6880951b8f3dd5 100644 --- a/backend/src/auth/user/authUsercontroller.ts +++ b/backend/src/auth/user/authUsercontroller.ts @@ -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') @@ -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; } diff --git a/backend/src/auth/user/authUserservice.ts b/backend/src/auth/user/authUserservice.ts index 2a0011bf8fa8f5f567015f52509e5d90abdf1f30..7a2eef2567a8f97fe362595464cd1c556527ec42 100644 --- a/backend/src/auth/user/authUserservice.ts +++ b/backend/src/auth/user/authUserservice.ts @@ -1,8 +1,7 @@ import { BadRequestException, - HttpException, - HttpStatus, Injectable, + UnauthorizedException, } from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { EncryptionService } from '../encryption/encryption.service'; @@ -21,7 +20,7 @@ export class AuthUserService { private encryptionService: EncryptionService, ) {} - async signUp(createAdminDto: CreateUserDto): Promise { + async signUp(createAdminDto: CreateUserDto): Promise { const loginAdminDto: LoginUserDto = { email: createAdminDto.email, password: createAdminDto.password, @@ -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 { 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, diff --git a/backend/src/auth/user/dto/user-signin-res.dto.ts b/backend/src/auth/user/dto/user-signin-res.dto.ts index 4491c55b0bd078e90126193a898d78882909aee4..f703702a5cea88e8a012fe3f785ae34023797c82 100644 --- a/backend/src/auth/user/dto/user-signin-res.dto.ts +++ b/backend/src/auth/user/dto/user-signin-res.dto.ts @@ -1,6 +1,8 @@ import { ApiProperty } from '@nestjs/swagger'; export class UserSigninResDto { + @ApiProperty() + userId: number; @ApiProperty() email: string; @ApiProperty() diff --git a/backend/src/user/entities/user.entity.ts b/backend/src/user/entities/user.entity.ts index 960373866c3af1efcc74beea36223052a0cdd00e..01e24044695acaaa7bc9ec6e61bc3c2329e5edcd 100644 --- a/backend/src/user/entities/user.entity.ts +++ b/backend/src/user/entities/user.entity.ts @@ -3,7 +3,7 @@ import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() - id: number; + userId: number; @Column({ unique: true, diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index 82ef1863eb46d96bc63b30aa3f308fb35fd101e2..c538fcd5a9b08737cf089a7fce6477951a7848b5 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -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) {