Files
leaudit-platform-frontend/app/config/oauth-secret.server.ts
T

70 lines
2.4 KiB
TypeScript

/**
* 🔒 服务器端专用:OAuth 敏感配置
*
* 此文件只在服务器端运行,确保环境变量在运行时读取
* Remix 会自动排除 .server.ts 文件不打包到客户端
*/
import { OAUTH_CONFIG } from './api-config';
// 用于控制日志输出(避免重复日志)
let hasLoggedSecret = false;
/**
* 从环境变量获取 OAuth Client Secret
* 在服务器运行时动态读取,不依赖构建时的环境变量注入
*/
export function getOAuthClientSecret(): string {
const secret = process.env.OAUTH_CLIENT_SECRET;
// 只在第一次调用时输出详细日志(避免启动时就输出)
if (!hasLoggedSecret) {
hasLoggedSecret = true;
console.log('🔍 [oauth-secret.server] 读取 OAUTH_CLIENT_SECRET:');
console.log(' - 值存在:', !!secret);
console.log(' - 值长度:', secret?.length || 0);
console.log(' - 值预览:', secret ? `${secret.substring(0, 10)}...` : 'undefined');
console.log(' - 是否为占位符:', secret === 'placeholder' || secret === 'none');
if (!secret || secret === 'placeholder' || secret === 'none') {
console.warn('⚠️ 警告:未设置有效的 OAUTH_CLIENT_SECRET 环境变量');
console.warn('⚠️ 当前值:', secret);
console.warn('⚠️ 包含 OAUTH 的环境变量:', Object.keys(process.env).filter(k => k.includes('OAUTH')));
console.warn('⚠️ 包含 SECRET 的环境变量:', Object.keys(process.env).filter(k => k.includes('SECRET')));
} else {
console.log('✅ [oauth-secret.server] OAUTH_CLIENT_SECRET 已成功读取');
}
}
return secret || '';
}
/**
* 获取服务器端 OAuth 配置
* 使用 api-config.ts 中根据端口号配置的 OAuth 配置
* 只有 clientSecret 从环境变量获取
*/
export function getServerOAuthConfigRuntime() {
const secret = getOAuthClientSecret();
// 使用 api-config.ts 中根据端口号配置的 OAuth 配置
// 只覆盖 clientSecret 为从环境变量读取的值
console.log('🔧 [oauth-secret.server] 使用端口配置的 OAuth 配置:', {
serverUrl: OAUTH_CONFIG.serverUrl,
clientId: OAUTH_CONFIG.clientId,
redirectUri: OAUTH_CONFIG.redirectUri,
appId: OAUTH_CONFIG.appId,
hasClientSecret: !!secret
});
return {
serverUrl: OAUTH_CONFIG.serverUrl!,
clientId: OAUTH_CONFIG.clientId!,
redirectUri: OAUTH_CONFIG.redirectUri!,
appId: OAUTH_CONFIG.appId!,
clientSecret: secret
};
}