more auth fixes

This commit is contained in:
zramsay 2025-03-11 17:38:40 -04:00
parent ba785cf0c6
commit 3214322e1b
4 changed files with 52 additions and 9 deletions

View File

@ -24,6 +24,15 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
// Check authentication - accept session OR header authentication
const isAuthenticated = !!session || !!headerUser;
// Log authentication attempt details
console.log('Authentication attempt:', {
hasCookie: !!req.cookies.get('next-auth.session-token') || !!req.cookies.get('__Secure-next-auth.session-token'),
hasSession: !!session,
hasHeaderUser: !!headerUser,
cookies: Array.from(req.cookies.getAll()).map(c => ({ name: c.name, value: c.name.includes('next-auth') ? '[REDACTED]' : c.value })),
allHeaders: Object.fromEntries(Array.from(req.headers.entries())),
});
if (!isAuthenticated) {
console.log('Unauthorized access attempt to analyze API');
return NextResponse.json(
@ -32,9 +41,10 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
);
}
// Determine user info from either source
const userId = headerUser?.id;
const userEmail = headerUser?.email;
// Use headers for user info if available, otherwise fall back to session
// This is likely our issue - we were checking for a session but not extracting user info from it
const userId = headerUser?.id || req.headers.get('x-user-id') || 'unknown';
const userEmail = headerUser?.email || req.headers.get('x-user-email') || 'unknown@example.com';
// Log authentication details
console.log('Authentication details:', {

View File

@ -13,6 +13,20 @@ const Page: React.FC = (): React.ReactElement => {
// Get auth session
const { data: session, status } = useSession()
const isAuthenticated = status === 'authenticated' && !!session
// Log session details for debugging
React.useEffect(() => {
if (session) {
console.log('Session available:', {
authenticated: isAuthenticated,
user: session.user ? {
name: session.user.name,
email: session.user.email,
id: session.user.id
} : 'No user data'
});
}
}, [session, isAuthenticated]);
const theme = getThemeColors(APP_CONFIG.theme)

View File

@ -5,17 +5,27 @@ import NextAuth from "next-auth";
// Helper to get basic authentication status from the cookie
export async function getSessionFromCookie(req: NextRequest) {
try {
// Basic check for auth cookie
// Log all cookies for debugging
console.log('All cookies:', Array.from(req.cookies.getAll()).map(c => c.name));
// Look for session cookie - check all possible formats
const sessionCookie = req.cookies.get('next-auth.session-token') ||
req.cookies.get('__Secure-next-auth.session-token');
req.cookies.get('__Secure-next-auth.session-token') ||
req.cookies.get('__Host-next-auth.session-token');
if (!sessionCookie) {
console.log('No session cookie found');
return null;
}
// For now, we'll just check if the cookie exists - we'll get actual user data from headers
console.log('Session cookie found:', sessionCookie.name);
// In a production setup, we would decode and verify the JWT
// But for simplicity, we'll just check if the cookie exists
return {
isAuthenticated: true
isAuthenticated: true,
// In Vercel, the headers will include user info if you're using next-auth
// We'll add fallback user extraction in the API route
};
} catch (error) {
console.error('Error getting session from cookie:', error);

View File

@ -45,18 +45,27 @@ export async function analyzeImage(
}
// Prepare headers with user data if available
const userHeaders: Record<string, string> = {};
const userHeaders: Record<string, string> = {
'Accept': 'application/json'
};
if (sessionData?.userId) {
userHeaders['x-user-id'] = sessionData.userId;
console.log(`Setting user ID header: ${sessionData.userId}`);
}
if (sessionData?.userEmail) {
userHeaders['x-user-email'] = sessionData.userEmail;
console.log(`Setting user email header: ${sessionData.userEmail}`);
}
// Log headers for debugging
console.log('Sending request with headers:', userHeaders);
const response = await fetch('/api/analyze', {
method: 'POST',
headers: userHeaders,
body: formData
body: formData,
credentials: 'include' // Important - include credentials (cookies)
})
if (!response.ok) {