Skip to content

Commit

Permalink
chore: auth/login 구현중 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
yubinquitous committed Nov 8, 2023
1 parent 064eceb commit 40411da
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 6 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "28.1.4",
"@types/multer": "^1.4.9",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
Expand Down
26 changes: 23 additions & 3 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Controller, Get, Logger, Query, Res } from '@nestjs/common';
import {
Body,
Controller,
Get,
Logger,
Patch,
Post,
Query,
Res,
UseInterceptors,
} from '@nestjs/common';
import { Auth42Service } from './auth-42.service';
import { AuthService } from './auth.service';
import { UserSigninResponseDto } from './dto/user-signin-response.dto';
import { FileInterceptor } from '@nestjs/platform-express';

@Controller('auth')
export class AuthController {
Expand All @@ -11,8 +22,8 @@ export class AuthController {
) {}
private readonly logger = new Logger(AuthController.name);

@Get('/signin')
async signin(@Query('code') code: string, @Res() res: any) {
@Post('/signin')
async signin(@Body('code') code: string, @Res() res: any) {
// code를 이용해 access token을 받아온다.
const accessToken = await this.auth42Service.getAccessToken(code);
// access token을 이용해 사용자 정보를 받아온다.
Expand Down Expand Up @@ -43,4 +54,13 @@ export class AuthController {
};
return res.send(userSigninResponseDto);
}

// TODO: AuthGuard
@Patch('/login')
@UseInterceptors(FileInterceptor('image'))
async login(@Body('nickname') nickname: string) {
// nickname validation
await this.authService.validateNickname(nickname);
//
}
}
14 changes: 12 additions & 2 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { User } from 'src/users/entities/user.entity';
import { UserRepository } from './../users/users.repository';
Expand Down Expand Up @@ -33,7 +33,7 @@ export class AuthService {
}

async generateJwtToken(user: User) {
const payload = { id: user.id, nickname: user.nickname };
const payload = { id: user.id, email: user.email };
// accessToken 생성
const accessToken = await this.jwtService.signAsync(payload);
// refreshToken 생성
Expand All @@ -42,4 +42,14 @@ export class AuthService {
await this.userRepository.update(user.id, { refreshToken });
return { jwtAccessToken: accessToken, jwtRefreshToken: refreshToken };
}

async validateNickname(nickname: string) {
const user = await this.userRepository.findOneByNickname(nickname);
if (user) {
throw new HttpException(
'이미 존재하는 닉네임입니다.',
HttpStatus.CONFLICT,
);
}
}
}
11 changes: 11 additions & 0 deletions src/common/pipes/positiveInt.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { HttpException, Injectable, PipeTransform } from '@nestjs/common';

@Injectable()
export class PositiveIntPipe implements PipeTransform {
transform(value: number) {
if (value < 0) {
throw new HttpException('value > 0', 400);
}
return value;
}
}
2 changes: 1 addition & 1 deletion src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Column, Entity, Unique } from 'typeorm';
@Entity()
@Unique(['nickname'])
export class User extends BaseEntity {
@Column({ default: null })
@Column({ unique: true })
@IsString()
@Length(1, 10)
@Matches(/^[--a-zA-Z0-9!]+$/)
Expand Down
4 changes: 4 additions & 0 deletions src/users/users.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ export class UserRepository extends Repository<User> {
constructor(@InjectRepository(User) private dataSource: DataSource) {
super(User, dataSource.manager);
}

async findOneByNickname(nickname: string) {
return await this.findOne({ where: { nickname } });
}
}

0 comments on commit 40411da

Please sign in to comment.