Something went wrong. Try again.
Monorepo for Aesthetic.Computer aesthetic.computer
Something went wrong. Try again.
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109// FF1 Devices (Netlify Function)// Stores FF1 device pairing info per user
import { authorize } from "../../backend/authorization.mjs";import { connect } from "../../backend/database.mjs";
function corsHeaders() { return { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization", };}
export async function handler(event) { if (event.httpMethod === "OPTIONS") { return { statusCode: 204, headers: corsHeaders(), body: "" }; }
const authHeader = event.headers.authorization || event.headers.Authorization; const user = await authorize({ authorization: authHeader }); if (!user || !user.sub) { return { statusCode: 401, headers: corsHeaders(), body: JSON.stringify({ success: false, error: "Unauthorized" }), }; }
const database = await connect(); const collection = database.db.collection("ff1Devices");
try { if (event.httpMethod === "GET") { const devices = await collection .find({ user: user.sub }) .sort({ updatedAt: -1 }) .toArray();
return { statusCode: 200, headers: corsHeaders(), body: JSON.stringify({ success: true, devices }), }; }
if (event.httpMethod === "POST") { let body; try { body = JSON.parse(event.body || "{}"); } catch (err) { return { statusCode: 400, headers: corsHeaders(), body: JSON.stringify({ success: false, error: "Invalid JSON body" }), }; }
const { topicID, apiKey, deviceId, label } = body || {}; if (!topicID || typeof topicID !== "string") { return { statusCode: 400, headers: corsHeaders(), body: JSON.stringify({ success: false, error: "Missing topicID" }), }; }
const now = new Date(); await collection.updateOne( { user: user.sub, topicID: topicID.trim() }, { $set: { user: user.sub, topicID: topicID.trim(), apiKey: apiKey || null, deviceId: deviceId || null, label: label || null, updatedAt: now, }, $setOnInsert: { createdAt: now, }, }, { upsert: true }, );
return { statusCode: 200, headers: corsHeaders(), body: JSON.stringify({ success: true }), }; }
return { statusCode: 405, headers: corsHeaders(), body: JSON.stringify({ success: false, error: "Method not allowed" }), }; } catch (err) { return { statusCode: 500, headers: corsHeaders(), body: JSON.stringify({ success: false, error: err.message }), }; } finally { await database.disconnect(); }}