MCPcopy Index your code
hub / github.com/simstudioai/sim / getOAuthToken

Function getOAuthToken

apps/sim/app/api/auth/oauth/utils.ts:444–490  ·  view source on GitHub ↗
(userId: string, providerId: string)

Source from the content-addressed store, hash-verified

442}
443
444export async function getOAuthToken(userId: string, providerId: string): Promise<string | null> {
445 const connections = await db
446 .select({
447 id: account.id,
448 accessToken: account.accessToken,
449 refreshToken: account.refreshToken,
450 accessTokenExpiresAt: account.accessTokenExpiresAt,
451 idToken: account.idToken,
452 scope: account.scope,
453 })
454 .from(account)
455 .where(and(eq(account.userId, userId), eq(account.providerId, providerId)))
456 .orderBy(desc(account.updatedAt))
457 .limit(1)
458
459 if (connections.length === 0) {
460 logger.warn(`No OAuth token found for user ${userId}, provider ${providerId}`)
461 return null
462 }
463
464 const credential = connections[0]
465
466 // Determine whether we should refresh: missing token OR expired token
467 const now = new Date()
468 const tokenExpiry = credential.accessTokenExpiresAt
469 const shouldAttemptRefresh =
470 !!credential.refreshToken && (!credential.accessToken || (tokenExpiry && tokenExpiry < now))
471
472 if (shouldAttemptRefresh) {
473 return performCoalescedRefresh({
474 accountId: credential.id,
475 providerId,
476 refreshToken: credential.refreshToken!,
477 userId,
478 })
479 }
480
481 if (!credential.accessToken) {
482 logger.warn(
483 `Access token is null and no refresh attempted or available for user ${userId}, provider ${providerId}`
484 )
485 return null
486 }
487
488 logger.info(`Found valid OAuth token for user ${userId}, provider ${providerId}`)
489 return credential.accessToken
490}
491
492/**
493 * Refreshes an OAuth token if needed based on credential information.

Callers 5

resolveAccessTokenFunction · 0.90
createSubscriptionFunction · 0.90
createSubscriptionFunction · 0.90
resolveOAuthCredentialFunction · 0.90
route.tsFile · 0.90

Calls 4

performCoalescedRefreshFunction · 0.85
infoMethod · 0.80
warnMethod · 0.65
eqFunction · 0.50

Tested by

no test coverage detected