Skip to content

Commit

Permalink
Merge pull request #59 from Boost-Coder/feature/catchError-#58
Browse files Browse the repository at this point in the history
[#58] apple 로그인 과정에서 token expire 에러 처리
  • Loading branch information
koomin1227 authored Apr 13, 2024
2 parents cc4cf49 + 3e65ff4 commit 3028fc7
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AppleLoginDto } from './appleLogin.dto';
import * as jwt from 'jsonwebtoken';
import * as jwksClient from 'jwks-rsa';
Expand All @@ -8,6 +8,7 @@ import { User } from '../Entity/user';
import { ConfigService } from '@nestjs/config';
import { SejongAuthDto } from './sejongAuth.dto';
import { UpdateUserInfoDto } from '../user/dto/update-user-info.dto';
import { TokenExpiredError } from 'jsonwebtoken';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -65,15 +66,22 @@ export class AuthService {
const kid = this.getkid(appleLoginDto.identityToken); // 토큰을 디코딩해서 kid를 가져옴
const applePublicKey = await this.getApplePublicKey(kid);

const isVerified: any = jwt.verify(
appleLoginDto.identityToken,
applePublicKey,
);

if (isVerified) {
return payloadClaimsJson.sub;
} else {
return null;
try {
const isVerified: any = jwt.verify(
appleLoginDto.identityToken,
applePublicKey,
);

if (isVerified) {
return payloadClaimsJson.sub;
} else {
return null;
}
} catch (e) {
if (e instanceof TokenExpiredError) {
throw new UnauthorizedException();
}
throw e;
}
}

Expand Down

0 comments on commit 3028fc7

Please sign in to comment.