Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
import AuthService, { assertAllowedRedirectUri } from "../services/auth.service";
import AuthService, { resolveRedirectUri } from "../services/auth.service";
import { AppError } from "../../errors/AppError";
import { CompleteSignupDto } from "../dtos/complete-signup.dto";
import { validate } from "class-validator";
Expand Down Expand Up @@ -328,8 +328,8 @@ class AuthController {
});
}

const validatedRedirectUri = assertAllowedRedirectUri(redirect_uri);
const result = await AuthService.exchangeKakaoToken(code, validatedRedirectUri);
const resolvedRedirectUri = resolveRedirectUri("KAKAO", redirect_uri);
const result = await AuthService.exchangeKakaoToken(code, resolvedRedirectUri);

res.status(200).json({
message: "์นด์นด์˜ค ๋กœ๊ทธ์ธ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.",
Expand Down Expand Up @@ -365,8 +365,8 @@ class AuthController {
});
}

const validatedRedirectUri = assertAllowedRedirectUri(redirect_uri);
const result = await AuthService.exchangeGoogleToken(code, validatedRedirectUri);
const resolvedRedirectUri = resolveRedirectUri("GOOGLE", redirect_uri);
const result = await AuthService.exchangeGoogleToken(code, resolvedRedirectUri);

res.status(200).json({
message: "๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.",
Expand Down Expand Up @@ -402,8 +402,8 @@ class AuthController {
});
}

const validatedRedirectUri = assertAllowedRedirectUri(redirect_uri);
const result = await AuthService.exchangeNaverToken(code, validatedRedirectUri);
const resolvedRedirectUri = resolveRedirectUri("NAVER", redirect_uri);
const result = await AuthService.exchangeNaverToken(code, resolvedRedirectUri);

res.status(200).json({
message: "๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.",
Expand Down
17 changes: 17 additions & 0 deletions src/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ export const assertAllowedRedirectUri = (redirectUri: string | undefined): strin
return redirectUri;
};

// ํ˜ธํ™˜์„ฑ fallback: body์˜ redirect_uri๊ฐ€ ์—†์„ ๋•Œ ํ™˜๊ฒฝ๋ณ€์ˆ˜์˜ provider๋ณ„ callback URL์„ ์‚ฌ์šฉํ•œ๋‹ค.
// ํ”„๋ก ํŠธ ๋‹จ๊ณ„์  ๋ฐ˜์˜์„ ์œ„ํ•œ ์ž„์‹œ ํ˜ธํ™˜ ๊ฒฝ๋กœ.
export const resolveRedirectUri = (
provider: "GOOGLE" | "KAKAO" | "NAVER",
bodyRedirectUri: string | undefined
): string => {
if (bodyRedirectUri) {
return assertAllowedRedirectUri(bodyRedirectUri);
}
const envKey = `${provider}_CALLBACK_URL` as const;
const fallback = process.env[envKey];
if (!fallback) {
throw new AppError("redirect_uri๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.", 400, "BadRequest");
}
return fallback;
};

class AuthService {
async generateTokens(user: any): Promise<Tokens> {
const accessToken = jwt.sign(
Expand Down
Loading