EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
auth.ts
Go to the documentation of this file.
1import { cookies } from 'next/headers';
2
3interface SessionData {
4 userId: string;
5 authenticated: boolean;
6 timestamp: number;
7 fieldpineData?: {
8 valid: number;
9 HomePage: string;
10 RmSystem: string;
11 };
12 apiKey?: string;
13}
14
15export async function getStoredAuth(): Promise<SessionData | null> {
16 try {
17 const cookieStore = await cookies();
18 const sessionCookie = cookieStore.get('fieldpine-session');
19
20 if (!sessionCookie) {
21 return null;
22 }
23
24 const sessionData: SessionData = JSON.parse(sessionCookie.value);
25
26 // Check if session is expired (8 hours)
27 if (Date.now() - sessionData.timestamp > 8 * 60 * 60 * 1000) {
28 return null;
29 }
30
31 return sessionData;
32 } catch (error) {
33 console.error('Error retrieving stored auth:', error);
34 return null;
35 }
36}
37
38export async function getStoredApiKey(): Promise<string | null> {
39 const auth = await getStoredAuth();
40 return auth?.apiKey || null;
41}
42
43export async function isAuthenticated(): Promise<boolean> {
44 const auth = await getStoredAuth();
45 return auth?.authenticated === true;
46}