31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
// routes/api/create/user_roles.ts
|
|
import { define } from "../../../utils.ts";
|
|
import { createUserRole } from "../../../services/userService.ts";
|
|
import { SessionCookie } from "../../auth/callback.ts";
|
|
|
|
export const handler = define.handlers({
|
|
async POST(ctx) {
|
|
try {
|
|
const stateData: SessionCookie = ctx.state.data;
|
|
const formData = await ctx.req.formData();
|
|
const sender = formData.get("sender");
|
|
const user_id = formData.get("user_id");
|
|
const role = formData.get("role");
|
|
if (stateData.role == "admin") {
|
|
await createUserRole(user_id as string, role as string);
|
|
}
|
|
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);
|
|
} catch (e) {
|
|
return new Response(JSON.stringify({ error: e }), { status: 500 });
|
|
}
|
|
},
|
|
});
|