Skip to content

Commit

Permalink
feat: 헤더 검증 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
kimploo committed Jan 5, 2024
1 parent 7cbbca8 commit 6005944
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const authFunc: RequestHandler = async (req, res, next) => {
const oldRefreshToken = decoded.refresh_token;
let user: User | null;
try {
user = await prisma.user.findFirst({
user = await prisma.user.findUnique({
where: {
kakaoRefreshToken: oldRefreshToken,
},
Expand All @@ -40,6 +40,15 @@ export const authFunc: RequestHandler = async (req, res, next) => {
return res.status(400).json(e);
}

// 헤더 검증
const authHeader = req.get('Authorization');
const isAuthorizedHeader = authHeader && authHeader === `Bearer ${user.kakaoAccessToken}`;

// 헤더가 있는지, 맞는지 검사
if (!isAuthorizedHeader) {
return res.status(401).send('Unauthorized');
}

// expired 아닌 경우 다시 재발행하지 않음
const isTokenExpired =
user.kakaoAccessTokenExpiresOn &&
Expand Down Expand Up @@ -117,14 +126,5 @@ export const authFunc: RequestHandler = async (req, res, next) => {
});
}

// 헤더 검증
const authHeader = req.get('Authorization');

// 헤더가 있는지, 맞는지 검사
if (authHeader && authHeader === `Bearer ${user.kakaoAccessToken}`) {
// If the authorization is correct, call the next middleware/function
return next();
} else {
return res.status(401).send('Unauthorized');
}
return next();
};
4 changes: 2 additions & 2 deletions src/types/kakaoRes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface KakaoLogoutSuccessRes {
id: bigint;
id: number | bigint;
}

export interface KakaoLogoutUnauthorizedRes {
Expand All @@ -17,7 +17,7 @@ export interface KakaoTokenRes {
}

export interface KakaoUserInfo {
id: number;
id: number | bigint;
connected_at: string;
properties: {
nickname: string | null;
Expand Down

0 comments on commit 6005944

Please sign in to comment.