2025-11-25 01:47:56 +07:00
|
|
|
import { FC, ReactElement, useEffect, useState, useRef } from 'react';
|
|
|
|
|
import { useNavigate } from 'react-router';
|
2025-11-25 02:34:52 +07:00
|
|
|
import { useAuthStore, supabase } from '@imphnen-frontend-service/service';
|
2025-11-25 01:47:56 +07:00
|
|
|
import { toast } from 'sonner';
|
|
|
|
|
|
|
|
|
|
const CallbackPage: FC = (): ReactElement => {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const { setSession } = useAuthStore();
|
|
|
|
|
const [isProcessing, setIsProcessing] = useState(true);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
const hasRunRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleCallback = async () => {
|
|
|
|
|
if (hasRunRef.current) {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] Already processed, skipping...');
|
2025-11-25 01:47:56 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
hasRunRef.current = true;
|
|
|
|
|
try {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] Processing OAuth callback...');
|
|
|
|
|
// console.log('[Callback] Current URL:', globalThis.location.href);
|
2025-11-25 01:47:56 +07:00
|
|
|
|
|
|
|
|
// Supabase client is configured with detectSessionInUrl: true
|
|
|
|
|
// This means Supabase automatically detects and processes OAuth tokens from the URL hash
|
|
|
|
|
// We just need to wait a moment for it to complete, then check for the session
|
|
|
|
|
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] Waiting for Supabase to process OAuth callback...');
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
2025-11-25 01:47:56 +07:00
|
|
|
|
|
|
|
|
// Get the session that Supabase automatically created from the URL hash
|
2025-11-25 19:59:08 +07:00
|
|
|
const {
|
|
|
|
|
data: { session: sessionData },
|
|
|
|
|
error: sessionError,
|
|
|
|
|
} = await supabase.auth.getSession();
|
2025-11-25 01:47:56 +07:00
|
|
|
|
|
|
|
|
if (sessionError) {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.error('[Callback] Session error:', sessionError);
|
2025-11-25 01:47:56 +07:00
|
|
|
throw new Error(sessionError.message || 'Failed to get session');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sessionData || !sessionData.user) {
|
2025-11-25 19:59:08 +07:00
|
|
|
throw new Error(
|
|
|
|
|
'No session found after OAuth callback. Please try logging in again.'
|
|
|
|
|
);
|
2025-11-25 01:47:56 +07:00
|
|
|
}
|
|
|
|
|
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] Supabase session established:', {
|
|
|
|
|
// userId: sessionData.user.id,
|
|
|
|
|
// email: sessionData.user.email,
|
|
|
|
|
// });
|
2025-11-25 01:47:56 +07:00
|
|
|
|
|
|
|
|
// Create/update user in the users table (for foreign key constraints)
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] Creating/updating user record...');
|
2025-11-25 01:47:56 +07:00
|
|
|
const { data: userData, error: upsertError } = await supabase
|
|
|
|
|
.from('users')
|
2025-11-25 19:59:08 +07:00
|
|
|
.upsert(
|
|
|
|
|
{
|
|
|
|
|
id: sessionData.user.id,
|
|
|
|
|
email: sessionData.user.email || '',
|
|
|
|
|
fullname:
|
|
|
|
|
sessionData.user.user_metadata?.full_name ||
|
|
|
|
|
sessionData.user.user_metadata?.name ||
|
|
|
|
|
sessionData.user.email?.split('@')[0] ||
|
|
|
|
|
'',
|
|
|
|
|
avatar: sessionData.user.user_metadata?.avatar_url || '',
|
|
|
|
|
is_active: true,
|
|
|
|
|
updated_at: new Date().toISOString(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onConflict: 'id',
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-11-25 01:47:56 +07:00
|
|
|
.select()
|
|
|
|
|
.single();
|
|
|
|
|
|
|
|
|
|
if (upsertError) {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.warn('[Callback] Failed to create user record:', upsertError);
|
2025-11-25 01:47:56 +07:00
|
|
|
// Don't throw - continue with login even if user record creation fails
|
|
|
|
|
} else {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] User record created/updated successfully');
|
2025-11-25 01:47:56 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store user-friendly data in Zustand for UI purposes
|
|
|
|
|
// Supabase now manages the actual auth session
|
|
|
|
|
// Use data from database if available, otherwise use OAuth metadata
|
|
|
|
|
const userRecord = userData || {
|
|
|
|
|
id: sessionData.user.id,
|
|
|
|
|
email: sessionData.user.email || '',
|
2025-11-25 19:59:08 +07:00
|
|
|
fullname:
|
|
|
|
|
sessionData.user.user_metadata?.full_name ||
|
|
|
|
|
sessionData.user.user_metadata?.name ||
|
|
|
|
|
sessionData.user.email?.split('@')[0] ||
|
|
|
|
|
'',
|
2025-11-25 01:47:56 +07:00
|
|
|
avatar: sessionData.user.user_metadata?.avatar_url || '',
|
|
|
|
|
phone_number: '',
|
|
|
|
|
birthdate: '',
|
|
|
|
|
gender: '',
|
|
|
|
|
is_active: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setSession({
|
|
|
|
|
token: {
|
|
|
|
|
access_token: sessionData.access_token,
|
|
|
|
|
refresh_token: sessionData.refresh_token || '',
|
|
|
|
|
},
|
|
|
|
|
user: {
|
|
|
|
|
id: userRecord.id,
|
|
|
|
|
email: userRecord.email,
|
|
|
|
|
fullname: userRecord.fullname,
|
|
|
|
|
phone_number: userRecord.phone_number || '',
|
|
|
|
|
avatar: userRecord.avatar || '',
|
|
|
|
|
birthdate: userRecord.birthdate || '',
|
|
|
|
|
gender: userRecord.gender || '',
|
|
|
|
|
is_active: userRecord.is_active,
|
|
|
|
|
location: userRecord.location,
|
|
|
|
|
bio: userRecord.bio,
|
|
|
|
|
skills: userRecord.skills,
|
|
|
|
|
role: {
|
|
|
|
|
id: '',
|
|
|
|
|
name: 'user',
|
|
|
|
|
permissions: [],
|
|
|
|
|
created_at: '',
|
|
|
|
|
updated_at: '',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] Session stored successfully');
|
2025-11-25 01:47:56 +07:00
|
|
|
toast.success('Login successful!');
|
|
|
|
|
setIsProcessing(false);
|
|
|
|
|
|
|
|
|
|
// Check if user has completed onboarding (has location)
|
|
|
|
|
// Use globalThis.location.replace for hard redirect to prevent history issues
|
|
|
|
|
if (userRecord.location) {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] User has completed onboarding, redirecting to dashboard...');
|
2025-11-25 01:47:56 +07:00
|
|
|
globalThis.location.replace('/dashboard');
|
|
|
|
|
} else {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.log('[Callback] User needs onboarding, redirecting...');
|
2025-11-25 01:47:56 +07:00
|
|
|
globalThis.location.replace('/onboarding/user');
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
2025-11-25 19:59:08 +07:00
|
|
|
// console.error('[Callback] Error:', err);
|
2025-11-25 01:47:56 +07:00
|
|
|
setError((err as Error).message);
|
|
|
|
|
setIsProcessing(false);
|
|
|
|
|
toast.error('An error occurred during login');
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
navigate('/auth/login');
|
|
|
|
|
}, 3000);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleCallback();
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, []); // Run only once on mount
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex justify-center items-center min-h-screen bg-gray-50 px-4">
|
|
|
|
|
<div className="bg-white w-full max-w-2xl p-8 rounded-2xl shadow-lg border border-red-200">
|
|
|
|
|
<div className="text-center mb-6">
|
|
|
|
|
<div className="text-red-500 text-5xl mb-4">⚠️</div>
|
|
|
|
|
<h2 className="text-2xl font-bold text-gray-900 mb-2">
|
|
|
|
|
GitHub Login Failed
|
|
|
|
|
</h2>
|
|
|
|
|
<p className="text-red-600 mb-4 whitespace-pre-line">{error}</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<p className="text-gray-600 text-sm mt-6 text-center">
|
|
|
|
|
Redirecting to login page in 3 seconds...
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2025-11-27 16:25:21 +07:00
|
|
|
<div className="flex justify-center items-center min-h-screen bg-gray-50 dark:bg-gray-950">
|
2025-11-25 01:47:56 +07:00
|
|
|
<div className="text-center">
|
|
|
|
|
<div className="inline-block animate-spin rounded-full h-12 w-12 border-b-2 border-primary-600 mb-4"></div>
|
2025-11-27 16:25:21 +07:00
|
|
|
<h2 className="text-xl font-semibold text-gray-900 dark:text-white mb-2">
|
2025-11-25 01:47:56 +07:00
|
|
|
Completing login...
|
|
|
|
|
</h2>
|
2025-11-27 16:25:21 +07:00
|
|
|
<p className="text-gray-600 dark:text-gray-400">Please wait</p>
|
2025-11-25 01:47:56 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CallbackPage;
|