'use client'; import { Button, Column, Heading, Icon, Loading, Row, Text } from '@umami/react-zen'; import { usePathname, useSearchParams } from 'next/navigation'; import { useEffect, useMemo, useState } from 'react'; import { LoginForm } from '@/app/login/LoginForm'; import { useApi, useLoginQuery, useMessages } from '@/components/hooks'; import { Logo } from '@/components/svg'; import { setReturnUrl } from '@/lib/return-url'; interface AuthorizationDetails { client: { id: string; name: string; uri?: string; logoUri?: string; source: string }; scopes: { scope: string; description: string }[]; redirectUri: string; resource: string; } const SCOPE_MESSAGE_KEYS: Record = { 'websites:read': 'oauthScopeWebsitesRead', 'analytics:read': 'oauthScopeAnalyticsRead', }; function getOAuthErrorMessage(error: unknown) { const record = error as { error_description?: string; message?: string; code?: string } | null; return record?.error_description || record?.message || null; } function Frame({ children }: { children: React.ReactNode }) { return ( umami {children} ); } export function OAuthAuthorizePage() { const { t, labels, messages } = useMessages(); const pathname = usePathname(); const searchParams = useSearchParams(); const params = useMemo(() => Object.fromEntries(searchParams.entries()), [searchParams]); const { user, isLoading } = useLoginQuery(); const { get, post, useQuery, useMutation } = useApi(); const [redirecting, setRedirecting] = useState(false); useEffect(() => { if (!isLoading && !user) { setReturnUrl(`${pathname}?${searchParams.toString()}`); } }, [isLoading, user, pathname, searchParams]); const details = useQuery({ queryKey: ['oauth-authorize', params], queryFn: () => get('/oauth/authorize', params), enabled: !!user, retry: false, }); const decide = useMutation({ mutationFn: (decision: 'approve' | 'deny') => post('/oauth/authorize', { ...params, decision }) as Promise<{ redirectUrl: string }>, onSuccess: ({ redirectUrl }) => { setRedirecting(true); window.location.assign(redirectUrl); }, }); if (isLoading) { return ; } if (!user) { return ( {t(messages.oauthLoginPrompt)} ); } if (details.isLoading) { return ; } if (details.error || !details.data) { return ( {t(messages.oauthInvalidRequest)} {getOAuthErrorMessage(details.error) && ( {getOAuthErrorMessage(details.error)} )} ); } const { client, scopes, redirectUri } = details.data; const redirectHost = (() => { try { return new URL(redirectUri).host || redirectUri; } catch { return redirectUri; } })(); const decisionError = getOAuthErrorMessage(decide.error); return ( {t(messages.oauthConsent, { name: client.name })} {client.uri && ( {client.uri} )} {t(messages.oauthPermissions)}
    {scopes.map(({ scope, description }) => (
  • {SCOPE_MESSAGE_KEYS[scope] ? t(messages[SCOPE_MESSAGE_KEYS[scope]]) : description}
  • ))}
{t(messages.oauthRedirectNotice, { url: redirectHost })} {t(messages.oauthSignedInAs, { username: user.username })} {decisionError && {decisionError}} {redirecting && {t(messages.oauthRedirecting)}}
); }