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
- Set the GOOGLE_REDIRECT_URI environment variable (e.g. https://yourapp.com/auth/google/callback).
- Or pass redirectUri explicitly: provider.getLoginUrl('https://yourapp.com/auth/google/callback', state).
- Or configure it in provider options: new MastraAuthGoogle({ redirectUri: '...' }).
- 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
- Set GOOGLE_REDIRECT_URI in all environments; it must exactly match a URI registered in Google Cloud Console.
- Pass the redirectUri explicitly at the call site if the provider isn't configured with one.
- Validate required OAuth env vars (client ID, secret, redirect URI) at process startup.
- Update the env var whenever the app's public base URL changes (new domain, added scheme, port change).
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
- Redirect URI is required for SSO. Set AUTH0_REDIRECT_URI or
- Redirect URI is required for SSO login
- Google client ID is required. Provide it in the options or s
- Google token exchange failed: ${error}
- Google token response did not include an ID token
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/ba2aee578fe7a19d.
Report an issue: GitHub.