Skip to content

Commit b3354be

Browse files
committed
fix: 애플 관련 키 못찾는 현상 해결
1 parent 79f5883 commit b3354be

1 file changed

Lines changed: 15 additions & 29 deletions

File tree

Firebase/functions/src/auth/apple.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ interface AppleTokenPayload {
1919
auth_time?: number; // 인증 시간
2020
}
2121

22-
// Apple 설정 불러오기
23-
const teamId = process.env.APPLE_TEAM_ID;
24-
const clientId = process.env.APPLE_CLIENT_ID;
25-
const keyId = process.env.APPLE_KEY_ID;
26-
const privateKey = (process.env.APPLE_PRIVATE_KEY || "").replace(/\\n/g, "\n");
22+
function getAppleConfiguration() {
23+
const teamId = process.env.APPLE_TEAM_ID;
24+
const clientId = process.env.APPLE_CLIENT_ID;
25+
const keyId = process.env.APPLE_KEY_ID;
26+
const privateKey = (process.env.APPLE_PRIVATE_KEY || "").replace(/\\n/g, "\n");
27+
28+
if (!teamId || !clientId || !keyId || !privateKey) {
29+
throw new HttpsError('internal', 'Missing Apple configuration');
30+
}
31+
32+
return { teamId, clientId, keyId, privateKey };
33+
}
2734

2835
export const requestAppleCustomToken = onCall({
2936
cors: true,
@@ -37,11 +44,6 @@ export const requestAppleCustomToken = onCall({
3744
throw new HttpsError('invalid-argument', 'ID token and authorization code are required');
3845
}
3946

40-
// Apple 설정 불러오기
41-
if (!teamId || !clientId || !keyId || !privateKey) {
42-
throw new HttpsError('internal', 'Missing Apple configuration');
43-
}
44-
4547
// // 1. Verify and decode the Apple ID token
4648
let decodedToken: AppleTokenPayload;
4749
try {
@@ -143,10 +145,6 @@ export const requestAppleRefreshToken = onCall({
143145
if (!authorizationCode || !uid) {
144146
throw new HttpsError("invalid-argument", "Authorization code and uid are required");
145147
}
146-
147-
if (!teamId || !clientId || !keyId || !privateKey) {
148-
throw new HttpsError("internal", "Missing Apple configuration");
149-
}
150148

151149
const refreshToken = await requestAppleRefreshTokenHelper(authorizationCode);
152150
console.log("appleRefreshToken:", refreshToken);
@@ -197,13 +195,7 @@ export const refreshAppleAccessToken = onCall({
197195
}
198196

199197
console.log("Successfully retrieved refresh token from Firestore");
200-
201-
if (!teamId || !clientId || !keyId || !privateKey) {
202-
throw new HttpsError(
203-
"internal",
204-
"Missing Apple configuration environment variables."
205-
);
206-
}
198+
const { teamId, clientId, keyId, privateKey } = getAppleConfiguration();
207199

208200
// Create client_secret JWT
209201
const clientSecret = jwt.sign({}, privateKey, {
@@ -265,15 +257,12 @@ export const revokeAppleAccessToken = onCall({
265257

266258
try {
267259
const { token } = request.data;
260+
const { teamId, clientId, keyId, privateKey } = getAppleConfiguration();
268261

269262
if (!token) {
270263
throw new HttpsError("invalid-argument", "Token is required");
271264
}
272265

273-
if (!teamId || !clientId || !keyId || !privateKey) {
274-
throw new HttpsError("internal", "Missing Apple configuration");
275-
}
276-
277266
// JWT 생성
278267
const clientSecret = jwt.sign({}, privateKey, {
279268
algorithm: "ES256",
@@ -323,10 +312,7 @@ export const revokeAppleAccessToken = onCall({
323312
});
324313

325314
export async function requestAppleRefreshTokenHelper(authorizationCode: string): Promise<string> {
326-
// Apple 설정 불러오기
327-
if (!teamId || !clientId || !keyId || !privateKey) {
328-
throw new HttpsError('internal', 'Missing Apple configuration');
329-
}
315+
const { teamId, clientId, keyId, privateKey } = getAppleConfiguration();
330316

331317
// JWT 생성
332318
const clientSecret = jwt.sign({}, privateKey, {

0 commit comments

Comments
 (0)