yikart/AiToEarn · error · AppException

ResponseCode.ChannelAuthPlatformMismatch

ResponseCode.ChannelAuthPlatformMismatch

Error message

ChannelAuthPlatformMismatch

What it means

Thrown in AuthService.completeCallback when the session stored in Redis was created for a different platform (session.platform !== platform argument). Each session is bound to one AccountType at creation; the callback URL path/platform must match it.

Source

Thrown at project/aitoearn-backend/apps/aitoearn-server/src/core/channels/auth/auth.service.ts:112

      expiresAt,
      authInstructions: integration.metadata.authInstructions,
    }
  }

  async completeCallback(
    platform: AccountType,
    callbackInput: Omit<AuthCallbackInput, 'session'>,
    sessionId: string,
  ): Promise<AuthCallbackResult> {
    const session = await this.redis.getChannelAuthSession<AuthSession>(sessionId)
    if (!this.isAccountAuthSessionRecord(session)) {
      throw new AppException(ResponseCode.ChannelAuthSessionInvalid)
    }
    if (this.isSessionExpired(session)) {
      throw new AppException(ResponseCode.ChannelAuthSessionInvalid)
    }
    if (session.platform !== platform) {
      throw new AppException(ResponseCode.ChannelAuthPlatformMismatch)
    }
    if (session.status !== ChannelAuthSessionStatus.Pending) {
      throw new AppException(ResponseCode.ChannelAuthSessionCompleted)
    }

    const provider = this.registry.getAuth(platform)
    const credentialResult = await provider.exchangeCode({
      ...callbackInput,
      session,
    })

    const credentialContext = credentialResult.accessToken
      ? this.toCredentialContext(credentialResult)
      : undefined
    const profile = credentialResult.profile
      ?? (credentialContext
        ? await provider.getProfile(credentialContext)
        : undefined)

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Ensure the redirect/callback URL used when generating the auth URL matches the platform that will deliver the callback.
  2. Never reuse a state/sessionId across platform connect flows; generate a fresh one per attempt.
  3. Check the callback route registration so each platform's callback hits its own handler with the correct platform value.
  4. If this is a test harness, start a new auth flow for the platform you are actually testing.

Example fix

// before: session started for douyin, callback posted to tiktok route
POST /api/channels/auth/callback/tiktok?state=<douyinSessionId>

// after: post to the platform the session was created for
POST /api/channels/auth/callback/douyin?state=<douyinSessionId>
Defensive patterns

Strategy: validation

Validate before calling

const expectedPlatform = getPlatformFromCallbackRoute(req)
const session = await redis.getChannelAuthSession<AuthSession>(sessionId)
if (session && session.platform !== expectedPlatform) {
  throw new Error(`Callback platform ${expectedPlatform} != session platform ${session.platform}`)
}

Type guard

function sessionMatchesPlatform(session: AuthSession | undefined, platform: AccountType): session is AuthSession & { platform: typeof platform } {
  return !!session && session.platform === platform
}

Try / catch

try {
  await completeCallback(platform, input, sessionId)
}
catch (e) {
  if (getErrorCode(e) === ResponseCode.ChannelAuthPlatformMismatch) {
    logError('callback routed to wrong platform handler', { platform, sessionId })
    redirect_to_correct_platform_callback(platform)
  }
}

Prevention

When it happens

Trigger: completeCallback is invoked with platform X while the session (identified by state/sessionId) was started for platform Y — e.g. the provider redirects to the wrong callback route, the state value is copied between different platform connect attempts, or the client hardcodes the platform in the callback URL.

Common situations: Sharing one redirect_uri template across platforms with the platform segment templated wrong; copy-pasting a callback URL from another platform integration test; a provider sending users to a generic callback that routes to the wrong handler.

Related errors


AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31). Data as JSON: /api/errors/f9e8ff5b893ada2e. Report an issue: GitHub.