mastra-ai/mastra · error

Redirect URI is required for Google SSO. Set GOOGLE_REDIRECT

Error message

Redirect URI is required for Google SSO. Set GOOGLE_REDIRECT_URI or pass redirectUri.

What it means

The SSO getLoginUrl implementation resolves the redirect URI from its argument or falls back to the provider's configured redirectUri (GOOGLE_REDIRECT_URI / options). Google's OAuth authorize endpoint requires a registered redirect_uri, so if neither source yields a value the library throws before building the login URL.

Source

Thrown at auth/google/src/auth-provider.ts:445

        return null;
      }

      return user;
    } catch {
      return null;
    }
  }

  private attachSSOProvider(): void {
    const self = this;

    (this as unknown as ISSOProvider<GoogleUser>).getLoginUrl = async function (
      redirectUri: string,
      state: string,
    ): Promise<string> {
      const actualRedirectUri = redirectUri ?? self.redirectUri;
      if (!actualRedirectUri) {
        throw new Error('Redirect URI is required for Google SSO. Set GOOGLE_REDIRECT_URI or pass redirectUri.');
      }

      const nonce = crypto.randomUUID();
      const signedState = await createStateToken(state, actualRedirectUri, nonce, self.cookiePassword);
      const oauthState = `${signedState}${getServerRedirectStateSuffix(state)}`;
      const params = new URLSearchParams({
        client_id: self.clientId,
        response_type: 'code',
        scope: self.scopes.join(' '),
        redirect_uri: actualRedirectUri,
        state: oauthState,
        nonce,
      });

      if (self.hostedDomain) {
        params.set('hd', self.hostedDomain);
      }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Set the GOOGLE_REDIRECT_URI environment variable (e.g. https://yourapp.com/auth/google/callback).
  2. Or pass redirectUri explicitly: provider.getLoginUrl('https://yourapp.com/auth/google/callback', state).
  3. Or configure it in provider options: new MastraAuthGoogle({ redirectUri: '...' }).
  4. Ensure the same URI is registered verbatim in Google Cloud Console OAuth client settings.

Example fix

// before
const url = await sso.getLoginUrl(undefined, state); // throws

// after
const url = await sso.getLoginUrl('https://myapp.com/auth/google/callback', state);
Defensive patterns

Strategy: validation

Validate before calling

const redirectUri = process.env.GOOGLE_REDIRECT_URI;
if (!redirectUri) {
  throw new Error('Set GOOGLE_REDIRECT_URI (must match the URI registered in Google Cloud Console)');
}

Try / catch

try {
  const url = await sso.getLoginUrl(redirectUri, state);
} catch (err) {
  if (err instanceof Error && err.message.includes('Redirect URI is required')) {
    console.error('Configure GOOGLE_REDIRECT_URI or pass redirectUri to getLoginUrl');
    process.exit(1);
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling getLoginUrl(undefined/empty, state) on the MastraAuthGoogle SSO interface while neither GOOGLE_REDIRECT_URI is set nor redirectUri was passed in provider options — the actualRedirectUri resolves to falsy.

Common situations: Deploying behind a new domain without updating GOOGLE_REDIRECT_URI; wiring the SSO interface manually and omitting the redirectUri argument; env var present locally in .env but missing on the host; forgetting to register the redirect URI in Google Cloud Console (which causes a different Google-side error, but the missing env var causes this one first).

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/ba2aee578fe7a19d. Report an issue: GitHub.