77 lines
1.8 KiB
TypeScript
77 lines
1.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { cookies } from "next/headers";
|
|
import {
|
|
validateToken,
|
|
isTokenExpired,
|
|
getTimeUntilExpiration,
|
|
} from "@/app/utils/auth";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const cookieStore = await cookies();
|
|
const token = cookieStore.get("auth_token")?.value;
|
|
|
|
if (!token) {
|
|
return NextResponse.json(
|
|
{
|
|
isAuthenticated: false,
|
|
message: "No authentication token found",
|
|
},
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Validate the token
|
|
const payload = await validateToken(token);
|
|
|
|
if (!payload) {
|
|
return NextResponse.json(
|
|
{
|
|
isAuthenticated: false,
|
|
message: "Invalid authentication token",
|
|
},
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
if (isTokenExpired(payload)) {
|
|
// Clear the expired cookie
|
|
cookieStore.delete("auth_token");
|
|
|
|
return NextResponse.json(
|
|
{
|
|
isAuthenticated: false,
|
|
message: "Authentication token has expired",
|
|
},
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Token is valid and not expired
|
|
const timeUntilExpiration = getTimeUntilExpiration(payload);
|
|
|
|
return NextResponse.json({
|
|
isAuthenticated: true,
|
|
user: {
|
|
email: payload.email,
|
|
role: payload.role,
|
|
},
|
|
tokenInfo: {
|
|
issuedAt: new Date(payload.iat * 1000).toISOString(),
|
|
expiresAt: new Date(payload.exp * 1000).toISOString(),
|
|
timeUntilExpiration: timeUntilExpiration, // in seconds
|
|
expiresInHours: Math.floor(timeUntilExpiration / 3600),
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Auth status check error:", error);
|
|
return NextResponse.json(
|
|
{
|
|
isAuthenticated: false,
|
|
message: "Internal server error",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|