1+ import { useEffect , useState } from 'react'
2+ import { useRouter } from 'next/router'
3+ import { createClient } from '@/utils/supabase/component'
4+ import { Card , CardContent } from "@/components/ui/card"
5+
6+ export default function AuthCallback ( ) {
7+ const router = useRouter ( )
8+ const [ message , setMessage ] = useState ( 'Processing authentication...' )
9+ const supabase = createClient ( )
10+
11+ useEffect ( ( ) => {
12+ // Only run once the router is ready and we have query params
13+ if ( ! router . isReady ) return
14+
15+ const handleAuthCallback = async ( ) => {
16+ try {
17+ // Check if we have a session already
18+ const { data : { session } } = await supabase . auth . getSession ( )
19+
20+ if ( session ) {
21+ setMessage ( 'You are logged in. Redirecting to home page...' )
22+ router . push ( '/' )
23+ return
24+ }
25+
26+ // Get the token from the URL if present
27+ const { token, type } = router . query
28+
29+ if ( token ) {
30+ setMessage ( 'Verifying your email...' )
31+
32+ // Exchange the token for a session
33+ const { error } = await supabase . auth . verifyOtp ( {
34+ token_hash : token as string ,
35+ type : ( type as string || 'signup' ) as 'signup' | 'email' | 'recovery' | 'invite' ,
36+ } )
37+
38+ if ( error ) {
39+ console . error ( 'Error verifying token:' , error )
40+ setMessage ( `Error: ${ error . message } ` )
41+ return
42+ }
43+
44+ setMessage ( 'Email verified! Redirecting to home page...' )
45+ router . push ( '/' )
46+ } else {
47+ // No token found, redirect to login
48+ setMessage ( 'No authentication token found. Redirecting to login...' )
49+ router . push ( '/login' )
50+ }
51+ } catch ( error ) {
52+ console . error ( 'Auth callback error:' , error )
53+ setMessage ( 'An error occurred during authentication. Please try logging in again.' )
54+ setTimeout ( ( ) => {
55+ router . push ( '/login' )
56+ } , 2000 )
57+ }
58+ }
59+
60+ handleAuthCallback ( )
61+ } , [ router . isReady , router . query , router , supabase ] )
62+
63+ return (
64+ < div className = "flex items-center justify-center min-h-screen bg-gray-50" >
65+ < Card className = "w-full max-w-md" >
66+ < CardContent className = "pt-6 text-center" >
67+ < div className = "mb-4" >
68+ < div className = "animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500 mx-auto" > </ div >
69+ </ div >
70+ < p > { message } </ p >
71+ </ CardContent >
72+ </ Card >
73+ </ div >
74+ )
75+ }
0 commit comments