@@ -4,12 +4,6 @@ import { createAuthServer } from './auth-server.js';
44import * as oauthClient from '@x/core/dist/auth/oauth-client.js' ;
55import type { Configuration } from '@x/core/dist/auth/oauth-client.js' ;
66import { getProviderConfig , getAvailableProviders } from '@x/core/dist/auth/providers.js' ;
7- import {
8- clearProviderClientIdOverride ,
9- getProviderClientIdOverride ,
10- hasProviderClientIdOverride ,
11- setProviderClientIdOverride ,
12- } from '@x/core/dist/auth/provider-client-id.js' ;
137import container from '@x/core/dist/di/container.js' ;
148import { IOAuthRepo } from '@x/core/dist/auth/repo.js' ;
159import { IClientRegistrationRepo } from '@x/core/dist/auth/client-repo.js' ;
@@ -80,24 +74,28 @@ function getClientRegistrationRepo(): IClientRegistrationRepo {
8074/**
8175 * Get or create OAuth configuration for a provider
8276 */
83- async function getProviderConfiguration ( provider : string ) : Promise < Configuration > {
77+ async function getProviderConfiguration ( provider : string , clientIdOverride ?: string ) : Promise < Configuration > {
8478 const config = getProviderConfig ( provider ) ;
85- const resolveClientId = ( ) : string => {
86- const override = getProviderClientIdOverride ( provider ) ;
87- if ( override ) {
88- return override ;
89- }
79+ const resolveClientId = async ( ) : Promise < string > => {
9080 if ( config . client . mode === 'static' && config . client . clientId ) {
9181 return config . client . clientId ;
9282 }
83+ if ( clientIdOverride ) {
84+ return clientIdOverride ;
85+ }
86+ const oauthRepo = getOAuthRepo ( ) ;
87+ const clientId = await oauthRepo . getClientId ( provider ) ;
88+ if ( clientId ) {
89+ return clientId ;
90+ }
9391 throw new Error ( `${ provider } client ID not configured. Please provide a client ID.` ) ;
9492 } ;
9593
9694 if ( config . discovery . mode === 'issuer' ) {
9795 if ( config . client . mode === 'static' ) {
9896 // Discover endpoints, use static client ID
9997 console . log ( `[OAuth] ${ provider } : Discovery from issuer with static client ID` ) ;
100- const clientId = resolveClientId ( ) ;
98+ const clientId = await resolveClientId ( ) ;
10199 return await oauthClient . discoverConfiguration (
102100 config . discovery . issuer ,
103101 clientId
@@ -137,7 +135,7 @@ async function getProviderConfiguration(provider: string): Promise<Configuration
137135 }
138136
139137 console . log ( `[OAuth] ${ provider } : Using static endpoints (no discovery)` ) ;
140- const clientId = resolveClientId ( ) ;
138+ const clientId = await resolveClientId ( ) ;
141139 return oauthClient . createStaticConfiguration (
142140 config . discovery . authorizationEndpoint ,
143141 config . discovery . tokenEndpoint ,
@@ -161,15 +159,13 @@ export async function connectProvider(provider: string, clientId?: string): Prom
161159 const providerConfig = getProviderConfig ( provider ) ;
162160
163161 if ( provider === 'google' ) {
164- const trimmedClientId = clientId ?. trim ( ) ;
165- if ( ! trimmedClientId ) {
162+ if ( ! clientId ) {
166163 return { success : false , error : 'Google client ID is required to connect.' } ;
167164 }
168- setProviderClientIdOverride ( provider , trimmedClientId ) ;
169165 }
170166
171167 // Get or create OAuth configuration
172- const config = await getProviderConfiguration ( provider ) ;
168+ const config = await getProviderConfiguration ( provider , clientId ) ;
173169
174170 // Generate PKCE codes
175171 const { verifier : codeVerifier , challenge : codeChallenge } = await oauthClient . generatePKCE ( ) ;
@@ -217,6 +213,10 @@ export async function connectProvider(provider: string, clientId?: string): Prom
217213 // Save tokens
218214 console . log ( `[OAuth] Token exchange successful for ${ provider } ` ) ;
219215 await oauthRepo . saveTokens ( provider , tokens ) ;
216+ if ( provider === 'google' && clientId ) {
217+ await oauthRepo . setClientId ( provider , clientId ) ;
218+ }
219+ await oauthRepo . clearError ( provider ) ;
220220
221221 // Trigger immediate sync for relevant providers
222222 if ( provider === 'google' ) {
@@ -282,33 +282,13 @@ export async function disconnectProvider(provider: string): Promise<{ success: b
282282 try {
283283 const oauthRepo = getOAuthRepo ( ) ;
284284 await oauthRepo . clearTokens ( provider ) ;
285- if ( provider === 'google' ) {
286- clearProviderClientIdOverride ( provider ) ;
287- }
288285 return { success : true } ;
289286 } catch ( error ) {
290287 console . error ( 'OAuth disconnect failed:' , error ) ;
291288 return { success : false } ;
292289 }
293290}
294291
295- /**
296- * Check if a provider is connected
297- */
298- export async function isConnected ( provider : string ) : Promise < { isConnected : boolean } > {
299- try {
300- const oauthRepo = getOAuthRepo ( ) ;
301- if ( provider === 'google' && ! hasProviderClientIdOverride ( provider ) ) {
302- return { isConnected : false } ;
303- }
304- const connected = await oauthRepo . isConnected ( provider ) ;
305- return { isConnected : connected } ;
306- } catch ( error ) {
307- console . error ( 'OAuth connection check failed:' , error ) ;
308- return { isConnected : false } ;
309- }
310- }
311-
312292/**
313293 * Get access token for a provider (internal use only)
314294 * Refreshes token if expired
@@ -326,6 +306,7 @@ export async function getAccessToken(provider: string): Promise<string | null> {
326306 if ( oauthClient . isTokenExpired ( tokens ) ) {
327307 if ( ! tokens . refresh_token ) {
328308 // No refresh token, need to reconnect
309+ await oauthRepo . setError ( provider , 'Missing refresh token. Please reconnect.' ) ;
329310 return null ;
330311 }
331312
@@ -338,6 +319,8 @@ export async function getAccessToken(provider: string): Promise<string | null> {
338319 tokens = await oauthClient . refreshTokens ( config , tokens . refresh_token , existingScopes ) ;
339320 await oauthRepo . saveTokens ( provider , tokens ) ;
340321 } catch ( error ) {
322+ const message = error instanceof Error ? error . message : 'Token refresh failed' ;
323+ await oauthRepo . setError ( provider , message ) ;
341324 console . error ( 'Token refresh failed:' , error ) ;
342325 return null ;
343326 }
@@ -350,23 +333,6 @@ export async function getAccessToken(provider: string): Promise<string | null> {
350333 }
351334}
352335
353- /**
354- * Get list of connected providers
355- */
356- export async function getConnectedProviders ( ) : Promise < { providers : string [ ] } > {
357- try {
358- const oauthRepo = getOAuthRepo ( ) ;
359- const providers = await oauthRepo . getConnectedProviders ( ) ;
360- const filteredProviders = providers . filter ( ( provider ) =>
361- provider === 'google' ? hasProviderClientIdOverride ( provider ) : true
362- ) ;
363- return { providers : filteredProviders } ;
364- } catch ( error ) {
365- console . error ( 'Get connected providers failed:' , error ) ;
366- return { providers : [ ] } ;
367- }
368- }
369-
370336/**
371337 * Get list of available providers
372338 */
0 commit comments