Skip to content

Commit

Permalink
feat : refreshtoken DB에 저장 #35
Browse files Browse the repository at this point in the history
- refreshtoken 테이블 엔티티 생성
- 로그인 시 User table에서 유저의 id, refreshtoken을 AuthRefreshToken 테이블에 저장.
  • Loading branch information
GeunH committed Dec 1, 2023
1 parent dced269 commit 55ea99e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
5 changes: 3 additions & 2 deletions be/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { JwtModule } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { UserModule } from "../user/user.module";
import { JwtStrategy } from "./strategy/jwt.strategy";
import { AuthRepository } from "./auth.repository";

@Module({
imports: [
Expand All @@ -18,7 +19,7 @@ import { JwtStrategy } from "./strategy/jwt.strategy";
forwardRef(() => UserModule),
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy],
providers: [AuthService, JwtStrategy, AuthRepository],
exports: [PassportModule],
})
export class AuthModule {}
export class AuthModule { }
14 changes: 14 additions & 0 deletions be/src/auth/auth.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DataSource, IsNull, Repository, Not, In } from "typeorm";
import {
ConflictException,
Injectable,
BadRequestException,
} from "@nestjs/common";
import { AuthRefreshTokenEntity } from "./entity/auth.refreshtoken.entity";

@Injectable()
export class AuthRepository extends Repository<AuthRefreshTokenEntity> {
constructor(private dataSource: DataSource) {
super(AuthRefreshTokenEntity, dataSource.createEntityManager());
}
}
27 changes: 18 additions & 9 deletions be/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ import { JwtService } from "@nestjs/jwt";
import axios from "axios";
import { LoginInfoDto } from "./dto/loginInfo.dto";
import { comparePasswords } from "src/utils/encryption.utils";
import { AuthRepository } from "./auth.repository";

@Injectable()
export class AuthService {
constructor(
private userRepository: UserRepository,
private jwtService: JwtService
private jwtService: JwtService,
private authRepository: AuthRepository
) { }
async login(loginInfoDto: LoginInfoDto) {
const data = await this.userRepository.findOne({ select: ["password"], where: { email: loginInfoDto.email, provider: "site" } })
const result = await comparePasswords(loginInfoDto.password, data["password"]);
if (result) return this.signin(loginInfoDto);
throw new UnauthorizedException();
try {
const result = await comparePasswords(loginInfoDto.password, data["password"]);
if (result) return this.signin(loginInfoDto);
else throw new UnauthorizedException();
} catch (err) {
throw new UnauthorizedException();
}
}

async NaverAuth(authorization: string) {
Expand Down Expand Up @@ -55,12 +61,11 @@ export class AuthService {
if (user) {
const payload = { id: user.id };
const accessToken = this.jwtService.sign(payload);

const refreshToken = this.jwtService.sign(payload, {
secret: "nibobnebob",
expiresIn: "7d",
});

await this.authRepository.upsert({ id: user.id, refreshToken: refreshToken }, ["id"]);
return { accessToken, refreshToken };
} else {
throw new NotFoundException(
Expand All @@ -74,9 +79,13 @@ export class AuthService {
const decoded = this.jwtService.verify(refreshToken, {
secret: "nibobnebob",
});
const payload = { id: decoded.id };
const accessToken = this.jwtService.sign(payload);
return { accessToken };
const result = this.authRepository.findOne({ where: { id: decoded.id } })
if (result) {
const payload = { id: decoded.id };
const accessToken = this.jwtService.sign(payload);
return { accessToken };
}
throw new HttpException("Invalid refresh token", HttpStatus.UNAUTHORIZED);
} catch (err) {
throw new HttpException("Invalid refresh token", HttpStatus.UNAUTHORIZED);
}
Expand Down
10 changes: 10 additions & 0 deletions be/src/auth/entity/auth.refreshtoken.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Entity, Column, PrimaryColumn } from 'typeorm';

@Entity("AuthRefreshToken")
export class AuthRefreshTokenEntity {
@PrimaryColumn()
id: number;

@Column({ type: 'varchar', length: 300 })
refreshToken: string
}

0 comments on commit 55ea99e

Please sign in to comment.