An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix: improve auth error handling in ErrorModal
Yoshiki Miurafc3f8b832b1a
diff --git a/components/chat.tsx b/components/chat.tsxindex 60ae6084..9c372440 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -1,6 +1,7 @@'use client'import { useEffect, useMemo, useRef, useState } from 'react'+import { useRouter } from 'next/navigation'import { useChat } from '@ai-sdk/react'import { DefaultChatTransport } from 'ai'@@ -39,6 +40,8 @@ export function Chat({savedMessages?: UIMessage[]query?: string}) {+ const router = useRouter()+// Generate a stable chatId on the client side// - If providedId exists (e.g., /search/[id]), use it for existing chats// - Otherwise, generate a new ID (e.g., / homepage for new chats)@@ -128,6 +131,12 @@ export function Chat({errorMessage.includes('too many requests') ||errorMessage.includes('daily limit')+ // Check for authentication errors+ const isAuthError =+ error.message?.includes('401') ||+ errorMessage.includes('unauthorized') ||+ errorMessage.includes('authentication required')+if (isRateLimit) {// Try to parse JSON error response for quality mode rate limitlet parsedError: {@@ -156,10 +165,7 @@ export function Chat({message: userMessage,details: undefined})- } else if (- error.message?.includes('401') ||- errorMessage.includes('unauthorized')- ) {+ } else if (isAuthError) {setErrorModal({open: true,type: 'auth',@@ -480,6 +486,11 @@ export function Chat({}: undefined}+ onAuthClose={() => {+ // Clear messages and navigate to root+ setMessages([])+ router.push('/')+ }}/></div>)diff --git a/components/error-modal.tsx b/components/error-modal.tsxindex 298e2565..eb53b2d7 100644--- a/components/error-modal.tsx+++ b/components/error-modal.tsx@@ -23,14 +23,21 @@ interface ErrorModalProps {details?: string}onRetry?: () => void+ onAuthClose?: () => void}export function ErrorModal({open,onOpenChange,error,- onRetry+ onRetry,+ onAuthClose}: ErrorModalProps) {+ const handleAuthClose = () => {+ onOpenChange(false)+ onAuthClose?.()+ }+const getErrorIcon = () => {switch (error.type) {case 'rate-limit':@@ -48,7 +55,7 @@ export function ErrorModal({case 'rate-limit':return 'Rate Limit Exceeded'case 'auth':- return 'Authentication Required'+ return 'Continue with Morphic'case 'forbidden':return 'Access Denied'default:@@ -64,7 +71,7 @@ export function ErrorModal({'You have made too many requests. Please wait a moment before trying again.')case 'auth':- return 'You need to sign in to continue using this feature.'+ return 'To use Morphic, sign in to your account or create a new one.'case 'forbidden':return 'You do not have permission to access this resource.'default:@@ -82,7 +89,16 @@ export function ErrorModal({}return (- <Dialog open={open} onOpenChange={onOpenChange}>+ <Dialog+ open={open}+ onOpenChange={open => {+ if (!open && error.type === 'auth') {+ handleAuthClose()+ } else {+ onOpenChange(open)+ }+ }}+ ><DialogContent className="sm:max-w-md"><DialogHeader><div className="mx-auto mb-4 flex size-12 items-center justify-center rounded-full bg-muted">