69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { cookies } from 'next/headers';
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { OperatorSession } from '../../../../components/ops/types';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const cookieStore = await cookies();
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
const opsToken = isProduction
|
|
? cookieStore.get('__Host-motorsignal_ops')?.value
|
|
: cookieStore.get('__Host-motorsignal_ops')?.value ??
|
|
cookieStore.get('motorsignal_ops')?.value;
|
|
|
|
if (!opsToken) {
|
|
return NextResponse.json(
|
|
{ detail: 'Authentication required.' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const baseUrl = process.env.MOTORSIGNAL_API_BASE_URL ?? 'https://api.motorsignal.app';
|
|
try {
|
|
const response = await fetch(`${baseUrl}/api/v1/ops/me`, {
|
|
method: 'GET',
|
|
headers: {
|
|
authorization: `Bearer ${opsToken}`,
|
|
accept: 'application/json',
|
|
},
|
|
cache: 'no-store',
|
|
});
|
|
|
|
if (response.status === 401) {
|
|
return NextResponse.json(
|
|
{ detail: 'Your Operations session has expired.' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
if (response.status === 403) {
|
|
return NextResponse.json(
|
|
{ detail: 'Access restricted.' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
if (!response.ok) {
|
|
return NextResponse.json(
|
|
{ detail: 'Operations is temporarily unavailable.' },
|
|
{ status: 503 }
|
|
);
|
|
}
|
|
|
|
const data = await response.json();
|
|
const session: OperatorSession = {
|
|
identity: data.identity ?? data.email ?? 'operator',
|
|
role: data.role === 'owner' ? 'owner' : 'operator',
|
|
status: data.status === 'expired' || data.status === 'revoked' ? data.status : 'active',
|
|
createdAt: data.created_at ?? data.createdAt,
|
|
lastActivityAt: data.last_activity_at ?? data.lastActivityAt,
|
|
expiresAt: data.expires_at ?? data.expiresAt,
|
|
environment: process.env.NODE_ENV === 'production' ? 'production' : 'development',
|
|
};
|
|
|
|
return NextResponse.json({ session }, { status: 200 });
|
|
} catch {
|
|
return NextResponse.json(
|
|
{ detail: 'Operations is temporarily unavailable.' },
|
|
{ status: 503 }
|
|
);
|
|
}
|
|
}
|