feat(auth): enable requireWorkspacePrincipal on individual project GET/PATCH routes to support desktop API keys
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { authSession } from "@/lib/auth/session-server";
|
||||
import { query } from '@/lib/db-postgres';
|
||||
import { requireWorkspacePrincipal } from "@/lib/auth/workspace-auth";
|
||||
import { query, queryOne } from '@/lib/db-postgres';
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
@@ -9,9 +9,18 @@ export async function GET(
|
||||
try {
|
||||
const { projectId } = await params;
|
||||
|
||||
const session = await authSession();
|
||||
if (!session?.user?.email) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
// 1. Authenticate the Workspace API key or Browser Session
|
||||
const principal = await requireWorkspacePrincipal(request);
|
||||
if (principal instanceof NextResponse) return principal;
|
||||
|
||||
// 2. Fetch user email from principal.userId
|
||||
const userRow = await queryOne<{ data: any }>(
|
||||
`SELECT data FROM fs_users WHERE id = $1 LIMIT 1`,
|
||||
[principal.userId]
|
||||
);
|
||||
const email = userRow?.data?.email;
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: 'User email not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
const rows = await query<{ id: string; data: any }>(`
|
||||
@@ -20,7 +29,7 @@ export async function GET(
|
||||
JOIN fs_users u ON u.id = p.user_id
|
||||
WHERE p.id = $1 AND u.data->>'email' = $2
|
||||
LIMIT 1
|
||||
`, [projectId, session.user.email]);
|
||||
`, [projectId, email]);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
@@ -44,9 +53,18 @@ export async function PATCH(
|
||||
const { projectId } = await params;
|
||||
const body = await request.json();
|
||||
|
||||
const session = await authSession();
|
||||
if (!session?.user?.email) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
// 1. Authenticate the Workspace API key or Browser Session
|
||||
const principal = await requireWorkspacePrincipal(request);
|
||||
if (principal instanceof NextResponse) return principal;
|
||||
|
||||
// 2. Fetch user email from principal.userId
|
||||
const userRow = await queryOne<{ data: any }>(
|
||||
`SELECT data FROM fs_users WHERE id = $1 LIMIT 1`,
|
||||
[principal.userId]
|
||||
);
|
||||
const email = userRow?.data?.email;
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: 'User email not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Fetch current data (verify ownership)
|
||||
@@ -56,7 +74,7 @@ export async function PATCH(
|
||||
JOIN fs_users u ON u.id = p.user_id
|
||||
WHERE p.id = $1 AND u.data->>'email' = $2
|
||||
LIMIT 1
|
||||
`, [projectId, session.user.email]);
|
||||
`, [projectId, email]);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
||||
|
||||
Reference in New Issue
Block a user