EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
route.ts
Go to the documentation of this file.
1import { NextRequest, NextResponse } from 'next/server';
2import jwt from 'jsonwebtoken';
3import { cookies } from 'next/headers';
4
5interface SessionData {
6 userId: string;
7 username: string;
8 authenticated: boolean;
9 role?: 'staff' | 'employee' | 'admin';
10 storeId?: string;
11 storeName?: string;
12 storeUrl?: string;
13 storeType?: 'management' | 'store';
14 timestamp: number;
15 apiKey?: string;
16 fieldpineData?: any;
17}
18
19/**
20 * Session endpoint - validates and returns current session
21 */
22export async function GET(request: NextRequest) {
23 try {
24 const cookieStore = await cookies();
25 const sessionCookie = cookieStore.get('fieldpine-session');
26
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)
33 });
34
35 if (!sessionCookie) {
36 console.log('[Auth Session] No session cookie found');
37 return NextResponse.json(
38 {
39 isAuthenticated: false,
40 error: 'No session found'
41 },
42 { status: 401 }
43 );
44 }
45
46 try {
47 const sessionData: SessionData = JSON.parse(sessionCookie.value);
48
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'
54 });
55
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
59
60 if (sessionAge > maxAge) {
61 // Session expired
62 console.log('[Auth Session] Session expired');
63 cookieStore.delete('fieldpine-session');
64
65 return NextResponse.json(
66 {
67 isAuthenticated: false,
68 error: 'Session expired'
69 },
70 { status: 401 }
71 );
72 }
73
74 // Return session data (without sensitive info)
75 return NextResponse.json({
76 isAuthenticated: true,
77 session: {
78 user: {
79 id: sessionData.userId,
80 name: sessionData.username,
81 role: sessionData.role || 'employee',
82 },
83 store: sessionData.storeId ? {
84 id: sessionData.storeId,
85 name: sessionData.storeName,
86 url: sessionData.storeUrl,
87 type: sessionData.storeType,
88 } : null,
89 authenticated: sessionData.authenticated,
90 },
91 });
92
93 } catch (parseError) {
94 console.log('[Auth] Invalid session cookie format');
95
96 // Clear invalid cookie
97 cookieStore.delete('fieldpine-session');
98
99 return NextResponse.json(
100 {
101 isAuthenticated: false,
102 error: 'Invalid session'
103 },
104 { status: 401 }
105 );
106 }
107
108 } catch (error) {
109 console.error('[Auth] Session validation error:', error);
110 return NextResponse.json(
111 { error: 'Session validation failed' },
112 { status: 500 }
113 );
114 }
115}