Skip to content

Commit 6d6d926

Browse files
committed
fix: Use dynamic environment config in auth-service for OAuth flows
1 parent a98bc47 commit 6d6d926

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

server/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ if (fs.existsSync(staticPath)) {
117117
VITE_NODE_ENV: process.env.VITE_NODE_ENV || 'production',
118118
VITE_OAUTH_WEB_CLIENT_ID: process.env.VITE_OAUTH_WEB_CLIENT_ID || '',
119119
VITE_OAUTH_WEB_CLIENT_SECRET: process.env.VITE_OAUTH_WEB_CLIENT_SECRET ? 'SET' : '',
120+
VITE_OAUTH_REDIRECT_URI: process.env.VITE_OAUTH_REDIRECT_URI || '',
120121

121122
// Development environment variables
122123
VITE_DEV_BACKEND_BASE_URL: process.env.VITE_DEV_BACKEND_BASE_URL || '',
@@ -204,6 +205,7 @@ if (fs.existsSync(staticPath)) {
204205
VITE_NODE_ENV: process.env.VITE_NODE_ENV || 'production',
205206
VITE_OAUTH_WEB_CLIENT_ID: process.env.VITE_OAUTH_WEB_CLIENT_ID || '',
206207
VITE_OAUTH_WEB_CLIENT_SECRET: process.env.VITE_OAUTH_WEB_CLIENT_SECRET ? 'SET' : '',
208+
VITE_OAUTH_REDIRECT_URI: process.env.VITE_OAUTH_REDIRECT_URI || '',
207209

208210
// Development environment variables
209211
VITE_DEV_BACKEND_BASE_URL: process.env.VITE_DEV_BACKEND_BASE_URL || '',

src/lib/auth-service.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getCurrentEnvironmentConfig } from './environment-switcher';
2+
13
interface TokenResponse {
24
access_token: string;
35
token_type: string;
@@ -7,7 +9,7 @@ interface TokenResponse {
79
}
810

911
interface TokenData {
10-
sToken: string;
12+
accessToken: string;
1113
tokenType: string;
1214
expiresAt: Date;
1315
refreshToken?: string;
@@ -243,11 +245,18 @@ class AuthService {
243245
sessionStorage.setItem('oauth_code_verifier', codeVerifier);
244246
sessionStorage.setItem('oauth_state', state);
245247

248+
// Get current environment configuration
249+
const envConfig = getCurrentEnvironmentConfig();
250+
console.log('🔐 [auth-service] Using environment config for login:', {
251+
loginBaseUrl: envConfig.loginBaseUrl,
252+
clientId: envConfig.oauthClientId
253+
});
254+
246255
// Build authorization URL
247256
const authUrl = await buildAuthorizationUrl(
248257
{
249-
clientId: OAUTH_CONFIG.webClientId,
250-
loginBaseUrl: ENV_URLS.loginBase,
258+
clientId: envConfig.oauthClientId,
259+
loginBaseUrl: envConfig.loginBaseUrl,
251260
redirectUri: OAUTH_CONFIG.redirectUri,
252261
scopes: OAUTH_CONFIG.scopes
253262
},
@@ -295,12 +304,19 @@ class AuthService {
295304
}
296305

297306
try {
307+
// Get current environment configuration for token exchange
308+
const envConfig = getCurrentEnvironmentConfig();
309+
console.log('🔑 [auth-service] Using environment config for token exchange:', {
310+
loginBaseUrl: envConfig.loginBaseUrl,
311+
clientId: envConfig.oauthClientId
312+
});
313+
298314
// Exchange code for tokens
299315
const tokenResponse = await exchangeCodeForToken(
300316
{
301-
clientId: OAUTH_CONFIG.webClientId,
302-
clientSecret: OAUTH_CONFIG.webClientSecret,
303-
loginBaseUrl: ENV_URLS.loginBase,
317+
clientId: envConfig.oauthClientId,
318+
clientSecret: envConfig.oauthClientSecret,
319+
loginBaseUrl: envConfig.loginBaseUrl,
304320
redirectUri: OAUTH_CONFIG.redirectUri,
305321
scopes: OAUTH_CONFIG.scopes
306322
},

src/lib/env.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,26 @@ export const OAUTH_CONFIG = {
152152
get webClientSecret() {
153153
return getOAuthClientSecret();
154154
},
155-
redirectUri: (() => {
156-
// Build redirect URI dynamically from current window location
157-
// This automatically works for localhost, dev, staging, and production environments
155+
get redirectUri() {
156+
// Priority:
157+
// 1. Runtime config (Azure environment variable)
158+
// 2. Build-time env (for local development)
159+
// 3. Dynamic from window.location.origin (fallback)
160+
161+
const runtimeRedirect = (window as any)?.runtimeConfig?.VITE_OAUTH_REDIRECT_URI;
162+
if (runtimeRedirect) {
163+
return runtimeRedirect;
164+
}
165+
166+
const buildTimeRedirect = import.meta.env.VITE_OAUTH_REDIRECT_URI;
167+
if (buildTimeRedirect) {
168+
return buildTimeRedirect;
169+
}
170+
171+
// Fallback: build dynamically from current window location
158172
const origin = window.location.origin;
159173
return `${origin}/account/callback`;
160-
})(),
174+
},
161175
scopes: [
162176
'openid',
163177
'profile',

0 commit comments

Comments
 (0)