Skip to content

Commit

Permalink
feat: 변경된 로그인 규칙 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondanythings committed Jul 14, 2024
1 parent 10f2e6f commit 3b2074f
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 140 deletions.
8 changes: 1 addition & 7 deletions prisma/ERD.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ erDiagram
DateTime createdAt
DateTime updatedAt
DateTime deletedAt "nullable"
String accountId
String password
String nickname UK
String email UK "nullable"
String phone UK "nullable"
String phone UK
DateTime birthday
Gender gender
DateTime verified "nullable"
String latestToken "nullable"
String latestProfileId FK "nullable"
Expand Down Expand Up @@ -99,13 +96,10 @@ erDiagram
- `createdAt`:
- `updatedAt`:
- `deletedAt`:
- `accountId`:
- `password`:
- `nickname`:
- `email`:
- `phone`:
- `birthday`:
- `gender`:
- `verified`:
- `latestToken`:
- `latestProfileId`:
Expand Down
5 changes: 1 addition & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ model User {
createdAt DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @default(now()) @updatedAt @db.Timestamp(6)
deletedAt DateTime? @db.Timestamp(6)
accountId String @db.VarChar(20)
password String @db.VarChar
nickname String @unique @db.VarChar(20)
email String? @unique @db.VarChar
phone String? @unique @db.VarChar
phone String @unique @db.VarChar
birthday DateTime @db.Date
gender Gender @default(NONE)
verified DateTime? @db.Timestamp(6)
latestToken String? @db.VarChar
Expand Down
3 changes: 0 additions & 3 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { ApiResult } from '@/shared/decorators/swagger/response.decorator'
import { Api } from '@/shared/dtos/api.dto'
import { CommonCodes } from '@/shared/errors/code/common.code'
import { ChangePasswordCodeResponseDTO } from './dtos/change-password-code-response.dto'
import { ApiException } from '@/shared/exceptions/exception.interface'

@Controller('auth')
@ApiTags('Auth')
Expand All @@ -50,8 +49,6 @@ export class AuthController {
async createUser(
@Body() createUserDTO: CreateUserDTO,
): Promise<Api<boolean>> {
if (!createUserDTO.email && !createUserDTO.phone)
throw ApiException.PLAIN_BAD_REQUEST('이메일 또는 전화번호는 필수에요.')
await this.authService.signup(createUserDTO)
return Api.OK(true)
}
Expand Down
12 changes: 2 additions & 10 deletions src/auth/auth.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,30 @@ export class AuthRepository {

async findOne(
createUserDTO: Partial<{
id: string
nickname: string
phone: string
email: string
}>,
) {
return this.database
.selectFrom('users')
.where((eb) =>
eb.or([
...(createUserDTO.id ? [eb('accountId', '=', createUserDTO.id)] : []),
...(createUserDTO.nickname
? [eb('nickname', '=', createUserDTO.nickname)]
: []),
...(createUserDTO.phone
? [eb('phone', '=', createUserDTO.phone)]
: []),
...(createUserDTO.email
? [eb('email', '=', createUserDTO.email)]
: []),
]),
)
.selectAll()
.executeTakeFirst()
}

async create(createUserDTO: CreateUserDTO & { password: string }) {
const { id, ...newUserData } = createUserDTO

async create(createUserDTO: CreateUserDTO) {
return this.database
.insertInto('users')
.values([{ accountId: id, ...newUserData, updatedAt: new Date() }])
.values([{ ...createUserDTO, updatedAt: new Date() }])
.returning('id')
.executeTakeFirstOrThrow()
}
Expand Down
65 changes: 34 additions & 31 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ import {
import { LoginRequestDTO } from '@/auth/dtos/login.dto'
import { GetUserByVidDTO } from '@/users/dtos/get-user-by-vid.dto'

import { EmailTemplateName } from '@/shared/constants/common.constant'

import { TokenPayload } from '@/shared/interfaces/token.interface'
import { AuthRepository } from '@/auth/auth.repository'

import { UserException } from '@/users/users.exception'
import { ChangePasswordCodeResponseDTO } from './dtos/change-password-code-response.dto'
import { AuthException } from './auth.exception'

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -87,21 +86,22 @@ export class AuthService {
`[풉 POOP] 인증번호 확인 문자입니다. \n 인증코드: ${newVerification.code}`,
getUserByVidDTO.vid,
)
} else if (getUserByVidDTO.type === VerificationType.EMAIL) {
await this.externalsService.sendEmail(
'[풉 POOP] 인증번호 확인 메일입니다.',
getUserByVidDTO.vid,
EmailTemplateName.CONFIRM_EMAIL,
[
{
key: 'code',
value: newVerification.code,
},
],
)
return true
}

return true
throw AuthException.NOTFOUND
// else if (getUserByVidDTO.type === VerificationType.EMAIL) {
// await this.externalsService.sendEmail(
// '[풉 POOP] 인증번호 확인 메일입니다.',
// getUserByVidDTO.vid,
// EmailTemplateName.CONFIRM_EMAIL,
// [
// {
// key: 'code',
// value: newVerification.code,
// },
// ],
// )
// }
}

@Transactional()
Expand All @@ -122,7 +122,7 @@ export class AuthService {
loginRequestDTO: LoginRequestDTO,
): Promise<VerifyingCodeResponseDTO> {
const userWhereCond = {
email: loginRequestDTO.id,
// email: loginRequestDTO.id,
phone: loginRequestDTO.id,
nickname: loginRequestDTO.id,
}
Expand Down Expand Up @@ -160,21 +160,24 @@ export class AuthService {
`POOP! \n 인증코드: ${code}`,
getUserByVidDTO.vid,
)
} else if (getUserByVidDTO.type === VerificationType.EMAIL) {
await this.externalsService.sendEmail(
'[PooP!] 계정인증코드입니다.',
getUserByVidDTO.vid,
EmailTemplateName.CONFIRM_EMAIL,
[
{
key: 'code',
value: code,
},
],
)
await this.redisService.setPasswordCode(foundUser.id, code)
return true
}
await this.redisService.setPasswordCode(foundUser.id, code)
return true
// else if (getUserByVidDTO.type === VerificationType.EMAIL) {
// await this.externalsService.sendEmail(
// '[PooP!] 계정인증코드입니다.',
// getUserByVidDTO.vid,
// EmailTemplateName.CONFIRM_EMAIL,
// [
// {
// key: 'code',
// value: code,
// },
// ],
// )
// }

throw AuthException.NOTFOUND
}

async verifyChangePasswordCode(
Expand Down
2 changes: 1 addition & 1 deletion src/auth/dtos/login.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class LoginRequestDTO {

@ApiProperty({
description: '로그인에 사용할 아이디',
example: '[email protected]',
example: '01099999999',
})
@IsString()
id: string
Expand Down
5 changes: 1 addition & 4 deletions src/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ export type User = {
createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>;
deletedAt: Timestamp | null;
accountId: string;
password: string;
nickname: string;
email: string | null;
phone: string | null;
phone: string;
birthday: Timestamp;
gender: Generated<Gender>;
verified: Timestamp | null;
latestToken: string | null;
latestProfileId: string | null;
Expand Down
13 changes: 13 additions & 0 deletions src/users/dtos/check-nickname.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsNotEmpty, IsString, Length } from 'class-validator'

export class CheckNicknameDTO {
@ApiProperty({
title: '닉네임 중복 체크',
example: 'raymond',
})
@IsString()
@IsNotEmpty()
@Length(2, 15)
nickname: string
}
49 changes: 23 additions & 26 deletions src/users/dtos/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { ApiProperty } from '@nestjs/swagger'
import {
IsEmail,
IsEnum,
IsNotEmpty,
IsOptional,
IsPhoneNumber,
Expand All @@ -10,7 +8,6 @@ import {
} from 'class-validator'
import { Transform } from 'class-transformer'

import { IsAccountId } from '@/shared/validators/is-account-id.validator'
import { IsAccountPassword } from '@/shared/validators/is-account-password.validator'
import { IsOnlyDate } from '@/shared/validators/is-date-string.validator'
import { IsUserId } from '@/shared/validators/is-user-id.validator'
Expand All @@ -36,8 +33,8 @@ interface CreateUserArgs {
}

export class CreateUserDTO {
@IsAccountId()
id: string
// @IsAccountId()
// id: string
@IsAccountPassword()
password: string

Expand All @@ -58,14 +55,6 @@ export class CreateUserDTO {
@MaxLength(16, { message: '닉네임은 16글자 이상으로 작성할 수 없어요.' })
nickname: string

@ApiProperty({
description: '사용자 성별',
type: Gender,
enum: Gender,
})
@IsEnum(Gender, { message: '유효하지 않은 성별이에요.' })
gender: Gender

@ApiProperty({
description: '사용자 전화번호',
example: '01093367663',
Expand All @@ -76,36 +65,44 @@ export class CreateUserDTO {
@IsPhoneNumber('KR', { message: '유효하지 않은 번호에요.' })
phone: string

@ApiProperty({
description: '사용자 이메일',
example: '[email protected]',
nullable: true,
})
@Transform(({ value }) => (value === '' ? null : value))
@IsOptional()
@IsEmail({}, { message: '유효하지 않은 이메일이에요.' })
email: string
// @ApiProperty({
// description: '사용자 성별',
// type: Gender,
// enum: Gender,
// })
// @IsEnum(Gender, { message: '유효하지 않은 성별이에요.' })
// gender: Gender

// @ApiProperty({
// description: '사용자 이메일',
// example: '[email protected]',
// nullable: true,
// })
// @Transform(({ value }) => (value === '' ? null : value))
// @IsOptional()
// @IsEmail({}, { message: '유효하지 않은 이메일이에요.' })
// email: string
}

export class CreateUserResponseDTO {
@IsUserId()
id: string

@ApiProperty({
description: '사용자 아이디',
description: '사용자 닉네임',
example: 'poopcoco123',
maxLength: 16,
minLength: 6,
})
@IsNotEmpty({ message: '아이디는 필수에요.' })
accountId: string
@IsNotEmpty({ message: '닉네임은 필수에요.' })
nickname: string

@IsYYYYMMDD()
birthday: Date

constructor(user: CreateUserArgs) {
this.id = user.id
this.accountId = user.accountId
this.nickname = user.nickname
this.birthday = user.birthday
}
}
23 changes: 10 additions & 13 deletions src/users/dtos/get-me.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { IsOnlyDate } from '@/shared/validators/is-date-string.validator'
import { ApiProperty } from '@nestjs/swagger'
import { Transform } from 'class-transformer'
import {
IsEmail,
IsEnum,
IsOptional,
IsPhoneNumber,
Expand Down Expand Up @@ -55,17 +54,17 @@ export class GetMeResponseDTO {
@Transform(({ value }) => (value === '' ? undefined : value))
@IsOptional()
@IsPhoneNumber('KR', { message: '유효하지 않은 번호에요.' })
phone?: string | null
phone: string | null

@ApiProperty({
description: '사용자 이메일',
example: '[email protected]',
nullable: true,
})
@Transform(({ value }) => (value === '' ? null : value))
@IsOptional()
@IsEmail({}, { message: '유효하지 않은 이메일이에요.' })
email?: string | null
// @ApiProperty({
// description: '사용자 이메일',
// example: '[email protected]',
// nullable: true,
// })
// @Transform(({ value }) => (value === '' ? null : value))
// @IsOptional()
// @IsEmail({}, { message: '유효하지 않은 이메일이에요.' })
// email?: string | null

@ApiProperty({
description: '인증 일자',
Expand All @@ -79,9 +78,7 @@ export class GetMeResponseDTO {
this.id = user.id
this.birthday = dayjs(user.birthday).format('YYYY-MM-DD')
this.nickname = user.nickname
this.gender = user.gender
this.phone = user.phone
this.email = user.email
this.verified = user.verified
this.latestProfileId = user.latestProfileId
}
Expand Down
Loading

0 comments on commit 3b2074f

Please sign in to comment.