Skip to content

An AI-powered search engine with a generative UI

feat: add authentication check and modal for chat access

Yoshiki Miura9b82fd3e9d1c
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index ff691a11..b98b1bec 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -49,6 +49,14 @@ export async function POST(req: Request) {
})
}
+ // Check if user is authenticated
+ if (!userId) {
+ return new Response('Authentication required', {
+ status: 401,
+ statusText: 'Unauthorized'
+ })
+ }
+
const cookieStore = await cookies()
const modelJson = cookieStore.get('selectedModel')?.value
@@ -76,7 +84,7 @@ export async function POST(req: Request) {
message,
model: selectedModel,
chatId,
- userId: userId!,
+ userId: userId,
trigger,
messageId,
abortSignal
diff --git a/components/auth-modal.tsx b/components/auth-modal.tsx
new file mode 100644
index 00000000..84b8b3c9
--- /dev/null
+++ b/components/auth-modal.tsx
@@ -0,0 +1,46 @@
+'use client'
+
+import Link from 'next/link'
+
+import { Button } from '@/components/ui/button'
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogHeader,
+ DialogTitle
+} from '@/components/ui/dialog'
+import { IconLogo } from '@/components/ui/icons'
+
+interface AuthModalProps {
+ open: boolean
+ onOpenChange: (open: boolean) => void
+}
+
+export function AuthModal({ open, onOpenChange }: AuthModalProps) {
+ return (
+ <Dialog open={open} onOpenChange={onOpenChange}>
+ <DialogContent className="sm:max-w-md">
+ <DialogHeader className="items-center text-center">
+ <div className="mx-auto mb-6 flex size-20 items-center justify-center rounded-full bg-muted">
+ <IconLogo className="size-14" />
+ </div>
+ <DialogTitle className="text-xl font-semibold">
+ Continue with Morphic
+ </DialogTitle>
+ <DialogDescription className="text-muted-foreground">
+ To use Morphic, sign in to your account or create a new one.
+ </DialogDescription>
+ </DialogHeader>
+ <div className="mt-6 space-y-3">
+ <Button asChild className="w-full" size="lg">
+ <Link href="/auth/sign-up">Sign Up</Link>
+ </Button>
+ <Button asChild variant="outline" className="w-full" size="lg">
+ <Link href="/auth/login">Sign In</Link>
+ </Button>
+ </div>
+ </DialogContent>
+ </Dialog>
+ )
+}
diff --git a/components/chat.tsx b/components/chat.tsx
index 7a2e677a..7ed2ecb4 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -17,8 +17,10 @@ import {
import { Model } from '@/lib/types/models'
import { cn } from '@/lib/utils'
+import { useAuthCheck } from '@/hooks/use-auth-check'
import { useFileDropzone } from '@/hooks/use-file-dropzone'
+import { AuthModal } from './auth-modal'
import { ChatMessages } from './chat-messages'
import { ChatPanel } from './chat-panel'
import { DragOverlay } from './drag-overlay'
@@ -45,6 +47,8 @@ export function Chat({
const [isAtBottom, setIsAtBottom] = useState(true)
const [uploadedFiles, setUploadedFiles] = useState<UploadedFile[]>([])
const [input, setInput] = useState('')
+ const [showAuthModal, setShowAuthModal] = useState(false)
+ const { isAuthenticated } = useAuthCheck()
const {
messages,
@@ -240,6 +244,12 @@ export function Chat({
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
+ // Check authentication before sending message
+ if (!isAuthenticated) {
+ setShowAuthModal(true)
+ return
+ }
+
const uploaded = uploadedFiles.filter(f => f.status === 'uploaded')
if (input.trim() || uploaded.length > 0) {
@@ -354,6 +364,7 @@ export function Chat({
scrollContainerRef={scrollContainerRef}
/>
<DragOverlay visible={isDragging} />
+ <AuthModal open={showAuthModal} onOpenChange={setShowAuthModal} />
</div>
)
}
diff --git a/hooks/use-auth-check.tsx b/hooks/use-auth-check.tsx
new file mode 100644
index 00000000..9a4a538e
--- /dev/null
+++ b/hooks/use-auth-check.tsx
@@ -0,0 +1,43 @@
+'use client'
+
+import { useEffect, useState } from 'react'
+import { createClient } from '@/lib/supabase/client'
+import { User } from '@supabase/supabase-js'
+
+export function useAuthCheck() {
+ const [user, setUser] = useState<User | null>(null)
+ const [loading, setLoading] = useState(true)
+
+ useEffect(() => {
+ const checkAuth = async () => {
+ try {
+ const supabase = createClient()
+ const {
+ data: { session }
+ } = await supabase.auth.getSession()
+ setUser(session?.user ?? null)
+ } catch (error) {
+ console.error('Error checking auth:', error)
+ setUser(null)
+ } finally {
+ setLoading(false)
+ }
+ }
+
+ checkAuth()
+
+ // Subscribe to auth changes
+ const supabase = createClient()
+ const {
+ data: { subscription }
+ } = supabase.auth.onAuthStateChange((event, session) => {
+ setUser(session?.user ?? null)
+ })
+
+ return () => {
+ subscription.unsubscribe()
+ }
+ }, [])
+
+ return { user, loading, isAuthenticated: !!user }
+}