Skip to content

Commit

Permalink
feat: signupDto 생성 후 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
sjy982 committed Nov 20, 2023
1 parent c81343b commit f17fdba
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 24 deletions.
35 changes: 35 additions & 0 deletions BackEnd/package-lock.json

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

2 changes: 2 additions & 0 deletions BackEnd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.16",
"@nestjs/typeorm": "^10.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"mysql2": "^3.6.3",
"nest-winston": "^1.9.4",
"reflect-metadata": "^0.1.13",
Expand Down
16 changes: 3 additions & 13 deletions BackEnd/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import { Body, Controller, Headers, Post, UseGuards } from '@nestjs/common';
import { AuthService } from './auth.service';
import { RefreshTokenGuard } from './guard/bearerToken.guard';
import { SignupDto } from './dto/signup.dto';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Post('signup')
signup(
@Body('userId') userId: string,
@Body('provider') provider: string,
@Body('nickname') nickname: string,
@Body('gender') gender: string,
@Body('birthdate') birthdate: Date,
@Body() body: SignupDto,
) {
return this.authService.registerWithUserIdAndProvider({
userId,
provider
}, {
nickname,
gender,
birthdate,
});
return this.authService.registerWithUserIdAndProvider(body);
}

@Post('token/access')
Expand Down
8 changes: 4 additions & 4 deletions BackEnd/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ProfileModel } from 'src/profiles/entities/profiles.entity';
import { ProfilesService } from 'src/profiles/profiles.service';
import { UserModel } from 'src/users/entities/users.entity';
import { UsersService } from 'src/users/users.service';
import { SignupDto } from './dto/signup.dto';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -43,11 +44,11 @@ export class AuthService {
return existingUser
}

async registerWithUserIdAndProvider(user: Pick<UserModel, 'userId' | 'provider'>, profile: Pick<ProfileModel, 'nickname' | 'gender' | 'birthdate'>) {
if(await this.profilesService.existByNickname(profile.nickname)) {
async registerWithUserIdAndProvider(signupInfo: SignupDto) {
if(await this.profilesService.existByNickname(signupInfo.nickname)) {
throw new BadRequestException("중복된 nickname 입니다.")
}
const newUser = await this.usersService.createUser(user, profile);
const newUser = await this.usersService.createUser(signupInfo);

return this.loginUser(newUser.profile.publicId);
}
Expand All @@ -74,7 +75,6 @@ export class AuthService {

rotateToken(token: string, isRefreshToken: boolean) {
const decoded = this.verifyToken(token);

if(decoded.type !== 'refresh') {
throw new UnauthorizedException('토큰 재발급은 Refresh 토큰으로만 가능합니다.!');
}
Expand Down
8 changes: 8 additions & 0 deletions BackEnd/src/auth/dto/signup.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IntersectionType, PickType } from "@nestjs/swagger";
import { ProfileModel } from "src/profiles/entities/profiles.entity";
import { UserModel } from "src/users/entities/users.entity";

class UserDto extends PickType(UserModel, ['userId', 'provider']) {}
class ProfileDto extends PickType(ProfileModel, ['nickname', 'gender', 'birthdate']) {}

export class SignupDto extends IntersectionType(UserDto, ProfileDto) {}
2 changes: 2 additions & 0 deletions BackEnd/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerSetting } from './config/swagger.config';
import { winstonLogger } from './config/winston.config';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: winstonLogger,
});
app.useGlobalPipes(new ValidationPipe);
SwaggerSetting(app);
await app.listen(3000);
}
Expand Down
10 changes: 10 additions & 0 deletions BackEnd/src/profiles/entities/profiles.entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IsDate, IsString } from 'class-validator';
import { PostModel } from 'src/posts/entities/posts.entity';
import { RecordModel } from 'src/records/entities/records.entity';
import { UserModel } from 'src/users/entities/users.entity';
Expand All @@ -18,12 +19,21 @@ export class ProfileModel {
id: number;

@Column({ unique: true })
@IsString({
message: 'nickname은 string 타입으로 입력해야합니다.',
})
nickname: string;

@Column({ nullable: false })
@IsString({
message: 'gender는 string 타입으로 입력해야합니다.',
})
gender: string;

@Column({ nullable: false })
@IsDate({
message: 'birthdate date 타입으로 입력해야합니다.',
})
birthdate: Date;

@CreateDateColumn()
Expand Down
3 changes: 1 addition & 2 deletions BackEnd/src/profiles/profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ProfilesService {
publicId,
}
});

return profile;
}

Expand All @@ -26,7 +26,6 @@ export class ProfilesService {
nickname,
}
});

if(nicknameExists) {
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions BackEnd/src/users/entities/users.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import {
JoinColumn,
} from 'typeorm';
import { ProfileModel } from 'src/profiles/entities/profiles.entity';
import { IsString } from 'class-validator';
@Entity()
export class UserModel {
@PrimaryGeneratedColumn()
id: number;

@Column()
@IsString({
message: 'userId는 string 타입을 입력해야합니다.'
})
userId: string;

@Column()
@IsString({
message: 'provider는 string 타입을 입력해야합니다.'
})
provider: string;

@OneToOne(() => ProfileModel, (profile) => profile.user, {
Expand Down
15 changes: 10 additions & 5 deletions BackEnd/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { UserModel } from './entities/users.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { ProfileModel } from 'src/profiles/entities/profiles.entity';
import { SignupDto } from 'src/auth/dto/signup.dto';

@Injectable()
export class UsersService {
Expand All @@ -11,11 +11,16 @@ export class UsersService {
private readonly usersRepository: Repository<UserModel>
){}

async createUser(user: Pick<UserModel, 'userId' | 'provider'>, profile: Pick<ProfileModel, 'nickname' | 'gender' | 'birthdate'>) {
async createUser(singupInfo: SignupDto) {
const profile = {
nickname: singupInfo.nickname,
gender: singupInfo.gender,
birthdate: singupInfo.birthdate,
}
const userObj = this.usersRepository.create({
userId: user.userId,
provider: user.provider,
profile,
userId: singupInfo.userId,
provider: singupInfo.provider,
profile
})

const newUesr = await this.usersRepository.save(userObj);
Expand Down

0 comments on commit f17fdba

Please sign in to comment.