1import { NextRequest, NextResponse } from 'next/server';
2import jwt from 'jsonwebtoken';
3import { cookies } from 'next/headers';
8 authenticated: boolean;
9 role?: 'staff' | 'employee' | 'admin';
13 storeType?: 'management' | 'store';
20 * Session endpoint - validates and returns current session
22export async function GET(request: NextRequest) {
24 const cookieStore = await cookies();
25 const sessionCookie = cookieStore.get('fieldpine-session');
27 console.log('[Auth Session] Request received:', {
28 hasCookie: !!sessionCookie,
29 cookieName: sessionCookie?.name,
30 cookieValueLength: sessionCookie?.value?.length,
31 protocol: request.headers.get('x-forwarded-proto'),
32 url: request.url.substring(0, 50)
36 console.log('[Auth Session] No session cookie found');
37 return NextResponse.json(
39 isAuthenticated: false,
40 error: 'No session found'
47 const sessionData: SessionData = JSON.parse(sessionCookie.value);
49 console.log('[Auth Session] Session parsed:', {
50 userId: sessionData.userId,
51 storeId: sessionData.storeId,
52 timestamp: new Date(sessionData.timestamp).toISOString(),
53 age: Math.round((Date.now() - sessionData.timestamp) / 1000 / 60) + ' minutes'
56 // Check if session has expired (8 hours)
57 const sessionAge = Date.now() - sessionData.timestamp;
58 const maxAge = 8 * 60 * 60 * 1000; // 8 hours in milliseconds
60 if (sessionAge > maxAge) {
62 console.log('[Auth Session] Session expired');
63 cookieStore.delete('fieldpine-session');
65 return NextResponse.json(
67 isAuthenticated: false,
68 error: 'Session expired'
74 // Return session data (without sensitive info)
75 return NextResponse.json({
76 isAuthenticated: true,
79 id: sessionData.userId,
80 name: sessionData.username,
81 role: sessionData.role || 'employee',
83 store: sessionData.storeId ? {
84 id: sessionData.storeId,
85 name: sessionData.storeName,
86 url: sessionData.storeUrl,
87 type: sessionData.storeType,
89 authenticated: sessionData.authenticated,
93 } catch (parseError) {
94 console.log('[Auth] Invalid session cookie format');
96 // Clear invalid cookie
97 cookieStore.delete('fieldpine-session');
99 return NextResponse.json(
101 isAuthenticated: false,
102 error: 'Invalid session'
109 console.error('[Auth] Session validation error:', error);
110 return NextResponse.json(
111 { error: 'Session validation failed' },