Skip to content

Commit

Permalink
refactor: left right type -> class, 에러처리 방법 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
de-novo committed Apr 14, 2024
1 parent 1127654 commit a2068eb
Show file tree
Hide file tree
Showing 41 changed files with 1,046 additions and 276 deletions.
297 changes: 154 additions & 143 deletions docs/ERD.md

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { CarModule } from './module/car.module';
import { DrivingModule } from './module/driving.module';
import { ParkingModule } from './module/parking.module';
import { SmsModule } from './module/sms.module';

@Module({
imports: [...defaultModules, AuthModule, SmsModule, CarModule],
imports: [
...defaultModules,
AuthModule,
SmsModule,
CarModule,
DrivingModule,
ParkingModule,
],
controllers: [AppController],
providers: [AppService],
})
Expand Down
4 changes: 2 additions & 2 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CurrentUser } from '@common/decorator/CurrentUser';
import { LoginOnly } from '@common/decorator/LoginOnly';
import { isLeft } from '@common/util/Either';
import { eitherToResponse, generateResponse } from '@common/util/Res';
import { TypedBody, TypedRoute } from '@nestia/core';
import { Controller, Res, UseGuards } from '@nestjs/common';
Expand Down Expand Up @@ -29,7 +28,8 @@ export class AuthController {
@Res({ passthrough: true }) res: Response,
) {
const result = await this.authService.login(dto);
if (isLeft(result)) {
if (result.isLeft()) {
result;
return eitherToResponse(result);
}
const { refreshToken, ...rest } = result.value;
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SmsModule } from '@/module/sms.module';
import { UserModule } from '@/module/user.module';
import { CertificationCodeRepository } from '@/repository/certificationCode.repository';
import { CertificationCodeRepository } from '@/repository/certification/certificationCode.repository';
import { Global, Module } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import {
Expand Down
4 changes: 2 additions & 2 deletions src/auth/guard/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export class AuthGuard implements CanActivate {

private validateRequest(request: any) {
const token = request.headers?.authorization?.split(' ')[1];
if (!token) return throwError(AUTH_ERROR.TOKEN_MISSING);
if (!token) return throwError(AUTH_ERROR.TOKEN_MISSING());

const user = this.jwtService.accessTokenVerify(token);
if (!user || !typia.is<JwtPayload>(user))
return throwError(AUTH_ERROR.TOKEN_INVALID);
return throwError(AUTH_ERROR.TOKEN_INVALID());
request.user = user;
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/auth/guard/refresh.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export class RefreshGuard implements CanActivate {

private async validateRequest(request: any) {
const token = this.extractToken(request);
if (!token) return throwError(AUTH_ERROR.TOKEN_MISSING);
if (!token) return throwError(AUTH_ERROR.TOKEN_MISSING());

const user = this.jwtService.refreshTokenVerify(token);
if (!user || !this.isValidPayload(user))
return throwError(AUTH_ERROR.TOKEN_INVALID);
return throwError(AUTH_ERROR.TOKEN_INVALID());

if (!(await this.isTokenValid(user.id, token))) {
return throwError(AUTH_ERROR.TOKEN_BLACKLISTED);
return throwError(AUTH_ERROR.TOKEN_BLACKLISTED());
}

request.user = user;
Expand Down
34 changes: 17 additions & 17 deletions src/auth/implement/auth.local.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AUTH_ERROR } from '@/constant/error/auth.error';
import { UserService } from '@/providers/user/user.service';
import { PrismaService } from '@common/prisma/prisma.service';
import { Either, isLeft, left, right } from '@common/util/Either';
import { Either, Left, Right } from '@common/util/Either';
import { Injectable } from '@nestjs/common';
import { CertificationCode } from '@prisma/client';

Expand Down Expand Up @@ -35,14 +35,14 @@ export class AuthLocalService {
const { account, password } = dto;
const user = await this.userService.findUser({ account });
if (!user) {
return left(AUTH_ERROR.AUTH_INVALID);
return Left.create(AUTH_ERROR.AUTH_INVALID());
}
const { password: hashedPassword } = user;
const isMatch = await this.authPasswordService.compare(
password,
hashedPassword,
);
if (isLeft(isMatch)) {
if (isMatch.isLeft()) {
return isMatch;
}
const accessToken = this.authJwtService.accessTokenSign({
Expand All @@ -56,7 +56,7 @@ export class AuthLocalService {
type: dto.type,
});

return right({
return Right.create({
accessToken,
refreshToken,
nickname: user.nickname,
Expand All @@ -74,27 +74,27 @@ export class AuthLocalService {
{ userId: string }
>
> {
const { certificationId, account, password, phone, contryCode, nickname } =
const { certificationId, account, password, phone, countryCode, nickname } =
dto;
const phonefull = `${contryCode}${phone}`;
const phonefull = `${countryCode}${phone}`;
const accountExists = await this.userService.checkUserExists({
account,
});
if (accountExists) {
return left(AUTH_ERROR.USER_ALREADY_EXISTS);
return Left.create(AUTH_ERROR.USER_ALREADY_EXISTS());
}

const phoneExists = await this.userService.checkUserExists({
phone,
});
if (phoneExists) {
return left(AUTH_ERROR.USER_ALREADY_EXISTS);
return Left.create(AUTH_ERROR.USER_ALREADY_EXISTS());
}
const certificationCode = await this.validateSignupCertificationCode({
certificationId,
phone: phonefull,
});
if (isLeft(certificationCode)) {
if (certificationCode.isLeft()) {
return certificationCode;
}

Expand All @@ -104,7 +104,7 @@ export class AuthLocalService {
{
account,
phone: phone,
contryCode: contryCode,
countryCode: countryCode,
password: hashedPassword,
nickname,
},
Expand All @@ -124,7 +124,7 @@ export class AuthLocalService {
where: { id: certificationId },
data: { status: 'SUCCESS' },
});
return right({ userId });
return Right.create({ userId });
});
}

Expand All @@ -143,20 +143,20 @@ export class AuthLocalService {
where: { id: certificationId },
});
if (!certificationCode) {
return left(AUTH_ERROR.CERTIFICATION_NOT_FOUND);
return Left.create(AUTH_ERROR.CERTIFICATION_NOT_FOUND());
}
if (certificationCode.status !== 'VERIFIED') {
return left(AUTH_ERROR.CERTIFICATION_INVALID);
return Left.create(AUTH_ERROR.CERTIFICATION_INVALID());
}
if (certificationCode.type !== 'SIGN_UP') {
return left(AUTH_ERROR.CERTIFICATION_INVALID);
return Left.create(AUTH_ERROR.CERTIFICATION_INVALID());
}
if (certificationCode.targetType !== 'PHONE') {
return left(AUTH_ERROR.CERTIFICATION_INVALID);
return Left.create(AUTH_ERROR.CERTIFICATION_INVALID());
}
if (certificationCode.target !== phone) {
return left(AUTH_ERROR.CERTIFICATION_INVALID);
return Left.create(AUTH_ERROR.CERTIFICATION_INVALID());
}
return right(certificationCode);
return Right.create(certificationCode);
}
}
4 changes: 2 additions & 2 deletions src/auth/provider/auth.jwt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class AuthJWTService implements BasicAuthJWTService {
secret: this.option.access_secret,
});
} catch (_) {
return throwError(AUTH_ERROR.TOKEN_INVALID);
return throwError(AUTH_ERROR.TOKEN_INVALID('토큰이 유효하지 않습니다.'));
}
}

Expand All @@ -46,7 +46,7 @@ export class AuthJWTService implements BasicAuthJWTService {
secret: this.option.refresh_secret,
});
} catch (_) {
return throwError(AUTH_ERROR.TOKEN_INVALID);
return throwError(AUTH_ERROR.TOKEN_INVALID('토큰이 유효하지 않습니다.'));
}
}
}
12 changes: 6 additions & 6 deletions src/auth/provider/auth.password.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AUTH_ERROR } from '@/constant/error/auth.error';
import { UserService } from '@/providers/user/user.service';
import { PrismaService } from '@common/prisma/prisma.service';
import { left, right } from '@common/util/Either';
import { Left, Right } from '@common/util/Either';
import { Inject, Injectable } from '@nestjs/common';
import * as bcrypt from 'bcrypt';
import { PASSWORD_OPTIONS } from '../auth.constant';
Expand All @@ -25,8 +25,8 @@ export class AuthPasswordService {

async compare(password: string, hashed: string) {
const isMatch = await bcrypt.compare(password, hashed);
if (!isMatch) return left(AUTH_ERROR.AUTH_INVALID);
return right(true);
if (!isMatch) return Left.create(AUTH_ERROR.AUTH_INVALID());
return Right.create(true);
}

// async changePassword(dto: Auth.ChangePassword.Request.Dto) {
Expand Down Expand Up @@ -73,7 +73,7 @@ export class AuthPasswordService {
// tx,
// );
// });
// return right({ email, id });
// return Right.create({ email, id });
// }

// private async changePasswordViaCurrentPassword(
Expand Down Expand Up @@ -103,12 +103,12 @@ export class AuthPasswordService {
// tx,
// );
// });
// return right({ email, id });
// return Right.create({ email, id });
// }

// private async checkUser(email: string) {
// const user = await this.userService.findUnique(email);
// if (isLeft(user)) return left(AuthError.User.USER_NOT_FOUND);
// return right(user.right);
// return Right.create(user.right);
// }
}
24 changes: 16 additions & 8 deletions src/auth/provider/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AUTH_ERROR } from '@/constant/error/auth.error';
import { Either, isLeft, left } from '@common/util/Either';
import { Either, Left } from '@common/util/Either';
import { Inject, Injectable } from '@nestjs/common';
import { Auth } from '@type/auth';
import { AuthError } from '@type/auth/error';
Expand Down Expand Up @@ -42,7 +42,9 @@ export class AuthService {
this.localService.login.bind(this.localService),
)(dto);
default:
return left(AUTH_ERROR.TYPE_NOT_SUPPORTED);
return Left.create(
AUTH_ERROR.TYPE_NOT_SUPPORTED('지원하지 않는 타입입니다.'),
);
}
}

Expand All @@ -61,7 +63,9 @@ export class AuthService {
case 'LOCAL':
return await this.localService.signup(dto);
default:
return left(AUTH_ERROR.TYPE_NOT_SUPPORTED);
return Left.create(
AUTH_ERROR.TYPE_NOT_SUPPORTED('지원하지 않는 타입입니다.'),
);
}
}

Expand Down Expand Up @@ -94,12 +98,14 @@ export class AuthService {
case 'PHONE':
const code = await this.generateCertificationCode();
return await this.phoneCertificationService.sendCertificationCode(
`${dto.contryCode}${dto.target}`,
`${dto.countryCode}${dto.target}`,
code,
dto.type,
);
default:
return left(AUTH_ERROR.TYPE_NOT_SUPPORTED);
return Left.create(
AUTH_ERROR.TYPE_NOT_SUPPORTED('지원하지 않는 타입입니다.'),
);
}
}

Expand All @@ -118,12 +124,14 @@ export class AuthService {
switch (dto.targetType) {
case 'PHONE':
return await this.phoneCertificationService.verifyCertificationCode(
`${dto.contryCode}${dto.target}`,
`${dto.countryCode}${dto.target}`,
dto.code,
dto.type,
);
default:
return left(AUTH_ERROR.TYPE_NOT_SUPPORTED);
return Left.create(
AUTH_ERROR.TYPE_NOT_SUPPORTED('지원하지 않는 타입입니다.'),
);
}
}

Expand All @@ -136,7 +144,7 @@ export class AuthService {
private processLogin(login: BasicAuthService['login']) {
return async (dto: Auth.Login.Request.Dto) => {
const result = await login(dto);
if (isLeft(result)) return result;
if (result.isLeft()) return result;
const { refreshToken, userId } = result.value;
await this.cacheService.setCache(userId, refreshToken);
return result;
Expand Down
Loading

0 comments on commit a2068eb

Please sign in to comment.