Skip to content

Commit

Permalink
Merge pull request #43 from maingockien01/Using-Cookie
Browse files Browse the repository at this point in the history
Set cookie for user
  • Loading branch information
AnhLe-Axel authored Feb 27, 2024
2 parents 1f126c6 + 1e5cfaa commit 05b7a97
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
22 changes: 19 additions & 3 deletions apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Body, Controller, Post, HttpCode, HttpStatus } from '@nestjs/common';
import {
Body,
Controller,
Post,
HttpCode,
HttpStatus,
Res,
} from '@nestjs/common';
import { AuthService } from './auth.service';
import { LogInDto } from '@team8/types/dtos/auth/login.dto';
import { SignUpDto } from '@team8/types/dtos/auth/signup.dto';
import { Response } from 'express';

@Controller()
export class AuthController {
Expand All @@ -14,7 +22,15 @@ export class AuthController {

@HttpCode(HttpStatus.OK)
@Post('login')
logIn(@Body() logInDto: LogInDto) {
return this.authService.logIn(logInDto);
async logIn(
@Body() logInDto: LogInDto,
@Res({ passthrough: true }) response: Response,
) {
const user = await this.authService.logIn(logInDto);
response.cookie('uid', user.uid);
response.cookie('username', user.username);
return {
message: 'success',
};
}
}
9 changes: 5 additions & 4 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@team8/types/dtos/auth';
import * as bcrypt from 'bcrypt';
import { CreateUserDto } from './createUser.dto';
import { User } from '../entities/user.entity';

@Injectable()
export class AuthService {
Expand All @@ -23,16 +24,16 @@ export class AuthService {
return result;
}

async logIn(dto: LogInDto): Promise<LogInRetDto> {
async logIn(dto: LogInDto): Promise<User> {
//TODO: Return message according to error
const user = await this.usersService.findOneByUsername(dto.username);
if (!(await bcrypt.compare(dto.password, user.hashPassword))) {
throw new UnauthorizedException();
}
const result = new LogInRetDto();
result.username = user.username;
// const result = new LogInRetDto();
// result.username = user.username;
// TODO: Generate a JWT and return it here
// instead of the user object
return result;
return user;
}
}

0 comments on commit 05b7a97

Please sign in to comment.