aethex-forge/api/user/set-realm.ts
sirpiglr 4f4025fe83 Improve Discord Activity UI with public endpoints and new realm setting
Refactors the client-side Activity page to use public API endpoints for feed and opportunities, handles authentication errors gracefully, and introduces a new API endpoint `/api/user/set-realm.ts` for updating user realm preferences.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 9203795e-937a-4306-b81d-b4d5c78c240e
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 1ada65e5-e282-425e-a4c8-c91528fd1e2c
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7c94b7a0-29c7-4f2e-94ef-44b2153872b7/9203795e-937a-4306-b81d-b4d5c78c240e/qPXTzuE
Replit-Helium-Checkpoint-Created: true
2025-12-08 22:13:52 +00:00

82 lines
2.3 KiB
TypeScript

import { getAdminClient } from "../_supabase.js";
const VALID_ARMS = [
"labs",
"gameforge",
"corp",
"foundation",
"devlink",
"nexus",
"staff",
];
export default async (req: Request) => {
if (req.method !== "POST" && req.method !== "PUT") {
return new Response(JSON.stringify({ error: "Method not allowed" }), {
status: 405,
headers: { "Content-Type": "application/json" },
});
}
try {
const authHeader = req.headers.get("Authorization");
if (!authHeader?.startsWith("Bearer ")) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
const token = authHeader.slice(7);
const supabase = getAdminClient();
const { data: userData, error: authError } = await supabase.auth.getUser(token);
if (authError || !userData.user) {
return new Response(JSON.stringify({ error: "Unauthorized" }), {
status: 401,
headers: { "Content-Type": "application/json" },
});
}
const body = await req.json() as { primary_arm?: string };
const { primary_arm } = body;
if (!primary_arm || !VALID_ARMS.includes(primary_arm)) {
return new Response(
JSON.stringify({
error: `Invalid primary_arm. Must be one of: ${VALID_ARMS.join(", ")}`,
}),
{
status: 400,
headers: { "Content-Type": "application/json" },
}
);
}
const { data, error } = await supabase
.from("user_profiles")
.update({ primary_arm })
.eq("id", userData.user.id)
.select("id, primary_arm")
.single();
if (error) {
console.error("[Set Realm API] Update error:", error);
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
return new Response(JSON.stringify({ success: true, primary_arm: data.primary_arm }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} catch (error: any) {
console.error("[Set Realm API] Unexpected error:", error);
return new Response(JSON.stringify({ error: error.message || "Internal server error" }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
};