| |
| |
| |
| import type { OAuthErrorCode } from "@midday/app-store/oauth-errors"; |
|
|
| export type { OAuthErrorCode }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function mapOAuthError(error: string | undefined): OAuthErrorCode { |
| if (!error) return "unknown_error"; |
|
|
| const errorLower = error.toLowerCase(); |
|
|
| |
| if ( |
| errorLower.includes("access_denied") || |
| errorLower.includes("user_denied") || |
| errorLower.includes("consent") || |
| errorLower.includes("cancelled") || |
| errorLower.includes("canceled") |
| ) { |
| return "access_denied"; |
| } |
|
|
| |
| if ( |
| errorLower.includes("missing_license") || |
| errorLower.includes("missing_app_license") || |
| errorLower.includes("license_required") || |
| errorLower.includes("no_license") |
| ) { |
| return "missing_license"; |
| } |
|
|
| |
| if ( |
| errorLower.includes("missing_system_admin") || |
| errorLower.includes("admin_right") || |
| errorLower.includes("insufficient_permission") || |
| errorLower.includes("invalid_scope") || |
| errorLower.includes("insufficient_scope") || |
| errorLower.includes("scope") |
| ) { |
| return "missing_permissions"; |
| } |
|
|
| |
| if ( |
| errorLower.includes("invalid_state") || |
| errorLower.includes("state_mismatch") || |
| errorLower.includes("state_expired") |
| ) { |
| return "invalid_state"; |
| } |
|
|
| return "unknown_error"; |
| } |
|
|
| |
| |
| |
| export function buildSuccessRedirect( |
| dashboardUrl: string, |
| provider: string, |
| source?: string, |
| fallbackPath = "/settings/apps", |
| ): string { |
| |
| if (source === "apps") { |
| return `${dashboardUrl}/oauth-callback?status=success`; |
| } |
|
|
| |
| const params = new URLSearchParams({ |
| connected: "true", |
| provider, |
| }); |
| return `${dashboardUrl}${fallbackPath}?${params.toString()}`; |
| } |
|
|
| |
| |
| |
| export function buildErrorRedirect( |
| dashboardUrl: string, |
| errorCode: string, |
| provider: string, |
| source?: string, |
| fallbackPath = "/settings/apps", |
| ): string { |
| |
| if (source === "apps") { |
| const params = new URLSearchParams({ |
| status: "error", |
| error: errorCode, |
| }); |
| return `${dashboardUrl}/oauth-callback?${params.toString()}`; |
| } |
|
|
| |
| const params = new URLSearchParams({ |
| connected: "false", |
| error: errorCode, |
| provider, |
| }); |
| return `${dashboardUrl}${fallbackPath}?${params.toString()}`; |
| } |
|
|