Repository navigation
Conversation
|
I'd prefer a union type of the 2 possible options. |
Lets see what @mandarini suggests, will adjust accordingly if needed! 💚🙌 |
|
Thank you for identifying and fixing this issue! The type mismatch you've found is indeed a real bug, the current types don't match the actual API behavior. However, I'd like to suggest an alternative approach that preserves type safety while fixing the issue. While making all fields optional does technically solve the TypeScript error, it loses important type information: // With optional fields, TypeScript can't help us:
const { data } = await getAuthorizationDetails(id)
console.log(data.client.name) // TypeScript says this is fine, but might crash at runtime The API actually returns two distinct response shapes depending on the authorization state:
{
authorization_id: string,
client: OAuthAuthorizationClient,
user: { id: string, email: string },
scope: string,
redirect_url?: string // optional
}
{
redirect_url: string
} I think we should use a union type like @Kroppeb suggested to represent both cases accurately. The codebase already has Here's the proposed change: // Keep OAuthAuthorizationDetails as-is (required fields for first-time auth)
export type OAuthAuthorizationDetails = {
/** The authorization ID */
authorization_id: string
/** Redirect URL - present if user already consented (can be used to trigger immediate redirect) */
redirect_url?: string
/** OAuth client requesting authorization */
client: OAuthAuthorizationClient
/** User object associated with the authorization */
user: {
/** User ID (UUID) */
id: string
/** User email */
email: string
}
/** Space-separated list of requested scopes */
scope: string
}
// Add a new type for the already-consented response
export type OAuthAlreadyConsentedDetails = {
/** URL to redirect the user back to the OAuth client */
redirect_url: string
}
// Update the response type to be a union
export type AuthOAuthAuthorizationDetailsResponse = RequestResult<
| OAuthAuthorizationDetails
| OAuthAlreadyConsentedDetails
> With this approach, TypeScript forces developers to handle both scenarios: const { data, error } = await supabase.auth.oauth.getAuthorizationDetails(authorizationId)
if (error || !data) {
// Handle error
return
}
// Type narrowing using discriminator
if ('authorization_id' in data) {
// TypeScript knows all fields are present here
console.log('Client:', data.client.name)
console.log('Scopes:', data.scope)
console.log('User:', data.user.email)
// Show consent UI...
} else {
// TypeScript knows only redirect_url is present
console.log('Already consented, redirecting to:', data.redirect_url)
// Handle redirect...
} The current types claim all fields are required, but the API sometimes returns only P.S. The JSDoc typo fix (redirect_uri → redirect_url) is perfect - that should definitely stay regardless of which approach we take! |
1600ea7 to
8bcb36b
Compare
|
@mandarini |
|
Hey @7ttp! Unfortunately, I think I'm going to close this PR in favor of a more comprehensive fix. The problem is on us. The issue with this PR: While you added the union type for the already-consented case, you missed the underlying bug in OAuthAuthorizationDetails itself. The API actually returns redirect_uri (not redirect_url) when consent is needed. This was incorrectly changed in PR #1891. What we're doing instead:
Your PR would have fixed the already-consented case but left the consent-needed case still broken with the wrong field name. Thanks again for reporting this! 💚 |
|
Fixed here: #2088 |
Summary
Make
OAuthAuthorizationDetailsfields optional to support already-consented OAuth responses.Problem
getAuthorizationDetailsis typed to returnAuthOAuthAuthorizationDetailsResponsewith required fields (authorization_id,client,user,scope).However, when a user has already approved access to an OAuth client, the API returns only
{ redirect_url: string }without the other fields, causing TypeScript type mismatches.Solution
Made
authorization_id,client,user, andscopeoptional inOAuthAuthorizationDetailstype.Also fixed a typo in the JSDoc (
redirect_uri→redirect_url).Related
getAuthorizationDetailsis wrong #2023