Skip to content

Commit

Permalink
Merge pull request #294 from boostcampwm-2024/Feature/#293_tokenVersi…
Browse files Browse the repository at this point in the history
…on_제거

Feature/#293 Access Token 인증 과정에서 tokenVersion 제거
  • Loading branch information
github-actions[bot] authored Dec 4, 2024
2 parents 50449c6 + 53ec923 commit 8f3e810
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 32 deletions.
3 changes: 0 additions & 3 deletions server/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ export class AuthController {
// DB에서 refresh token 삭제
await this.authService.removeRefreshToken(user.id);

// 사용자의 token version 증가
await this.authService.increaseTokenVersion(user);

// 쿠키 삭제
this.authService.clearCookie(req.res);
}
Expand Down
7 changes: 0 additions & 7 deletions server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export class AuthService {
return this.jwtService.sign({
sub: user.id,
email: user.email,
tokenVersion: await this.increaseTokenVersion(user),
});
}

Expand All @@ -87,12 +86,6 @@ export class AuthService {
return refreshToken;
}

async increaseTokenVersion(user: User): Promise<number> {
const tokenVersion = user.tokenVersion + 1;
await this.userModel.updateOne({ id: user.id }, { tokenVersion });
return tokenVersion;
}

async login(user: User, res: Response): Promise<UserDto> {
const accessToken = await this.generateAccessToken(user);
const refreshToken = await this.generateRefreshToken(user.id);
Expand Down
15 changes: 1 addition & 14 deletions server/src/auth/guards/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { Injectable, ExecutionContext, UnauthorizedException } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
import { AuthService } from "../auth.service";
import { JwtService } from "@nestjs/jwt";

@Injectable()
export class JwtAuthGuard extends AuthGuard("jwt") {
constructor(
private readonly authService: AuthService,
private readonly jwtService: JwtService,
) {
constructor() {
super();
}

Expand All @@ -22,14 +17,6 @@ export class JwtAuthGuard extends AuthGuard("jwt") {

const canActivate = (await super.canActivate(context)) as boolean;

// Access Token의 tokenVersion과 사용자의 tokenVersion 일치 여부 확인
const decodedToken = this.jwtService.decode(token) as { sub: string; tokenVersion: number };
const user = await this.authService.findById(decodedToken.sub);

if (!user || user.tokenVersion !== decodedToken.tokenVersion) {
throw new UnauthorizedException("Invalid token version");
}

return canActivate;
}
}
3 changes: 0 additions & 3 deletions server/src/auth/schemas/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ export class User {
@Prop({ required: true })
name: string;

@Prop({ required: true, default: () => 0 })
tokenVersion: number;

@Prop()
refreshToken: string;

Expand Down
1 change: 0 additions & 1 deletion server/src/auth/test/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe("AuthController", () => {
validateUser: jest.fn(),
getProfile: jest.fn(),
refresh: jest.fn(),
increaseTokenVersion: jest.fn(),
isValidEmail: jest.fn(),
};

Expand Down
4 changes: 0 additions & 4 deletions server/src/auth/test/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe("AuthService", () => {
email: "[email protected]",
password: "hashedPassword",
name: "Test User",
tokenVersion: 0,
};

const mockUserModel = {
Expand Down Expand Up @@ -139,7 +138,6 @@ describe("AuthService", () => {
id: "mockNanoId123",
email: "[email protected]",
name: "Test User",
tokenVersion: 0,
};

const mockResponse = {
Expand All @@ -152,7 +150,6 @@ describe("AuthService", () => {
expect(jwtService.sign).toHaveBeenCalledWith({
sub: user.id,
email: user.email,
tokenVersion: user.tokenVersion + 1,
});
expect(mockResponse.cookie).toHaveBeenCalledWith("refreshToken", expect.any(String), {
httpOnly: true,
Expand Down Expand Up @@ -234,7 +231,6 @@ describe("AuthService", () => {
expect(jwtService.sign).toHaveBeenCalledWith({
sub: mockUser.id,
email: mockUser.email,
tokenVersion: 1,
});
expect(mockResponse.header).toHaveBeenCalledWith("Authorization", `Bearer test-token`);
expect(result).toEqual({
Expand Down

0 comments on commit 8f3e810

Please sign in to comment.