96 lines
3.1 KiB
TypeScript
96 lines
3.1 KiB
TypeScript
import { define } from "../../../utils.ts";
|
|
import { SessionCookie } from "../../auth/callback.ts";
|
|
import { updateUser } from "../../../services/userService.ts";
|
|
|
|
export const handler = define.handlers({
|
|
POST: async (ctx) => {
|
|
const stateData: SessionCookie = ctx.state.data;
|
|
const { user_id } = stateData;
|
|
|
|
const contentType = ctx.req.headers.get("content-type") || "";
|
|
let data: Record<string, string | null> = {};
|
|
let sender: string | null = null;
|
|
let isJsonRequest = false;
|
|
|
|
// Handle both JSON and form data
|
|
if (contentType.includes("application/json")) {
|
|
// Handle JSON data (from WelcomeBar encryption setup)
|
|
isJsonRequest = true;
|
|
const jsonData = await ctx.req.json();
|
|
data = jsonData;
|
|
// For JSON requests, use the userId from the request or fallback to session
|
|
const targetUserId = jsonData.userId || user_id;
|
|
if (targetUserId !== user_id) {
|
|
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
|
status: 403,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
} else {
|
|
// Handle form data (from user profile forms)
|
|
const form = await ctx.req.formData();
|
|
sender = form.get("sender")?.toString() || null;
|
|
|
|
// Extract form fields
|
|
data = {
|
|
name: form.get("name")?.toString() ?? "",
|
|
email: form.get("email")?.toString() ?? "",
|
|
phone: form.get("phone")?.toString() ?? "",
|
|
address_1: form.get("address_1")?.toString() ?? "",
|
|
address_2: form.get("address_2")?.toString() ?? "",
|
|
city: form.get("city")?.toString() ?? "",
|
|
state: form.get("state")?.toString() ?? "",
|
|
zip: form.get("zip")?.toString() ?? "",
|
|
country: form.get("country")?.toString() ?? "",
|
|
encrypted_data: form.get("encrypted_data")?.toString() ?? null,
|
|
};
|
|
}
|
|
|
|
// Extract fields for database update
|
|
const name = data.name ?? "";
|
|
const email = data.email ?? "";
|
|
const phone = data.phone ?? "";
|
|
const address_1 = data.address_1 ?? "";
|
|
const address_2 = data.address_2 ?? "";
|
|
const city = data.city ?? "";
|
|
const state = data.state ?? "";
|
|
const zip = data.zip ?? "";
|
|
const country = data.country ?? "";
|
|
const encrypted_data = data.encrypted_data ?? null;
|
|
|
|
// Update user profile in DB
|
|
await updateUser(user_id, {
|
|
name,
|
|
email,
|
|
phone,
|
|
address_1,
|
|
address_2,
|
|
city,
|
|
state,
|
|
zip,
|
|
country,
|
|
encrypted_data,
|
|
});
|
|
|
|
// Return appropriate response based on request type
|
|
if (isJsonRequest) {
|
|
// Return JSON response for JSON requests
|
|
return new Response(JSON.stringify({ success: true }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
} else {
|
|
// Return redirect for form requests
|
|
const senderPath = sender?.toString() || "/";
|
|
let redirectUrl = senderPath;
|
|
try {
|
|
if (senderPath.startsWith("/")) {
|
|
const url = new URL(ctx.req.url);
|
|
redirectUrl = url.origin + senderPath;
|
|
}
|
|
} catch (_) {}
|
|
return Response.redirect(redirectUrl, 303);
|
|
}
|
|
},
|
|
});
|