Skip to content

Commit

Permalink
feat: add invitation management api route
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoidPhantom committed Sep 25, 2024
1 parent 6912df0 commit 1d555c9
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions server/api/auth/invitation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
serverSupabaseUser,
serverSupabaseServiceRole,
} from "#supabase/server";
import type { Database } from "~~/supabase/types";

export default defineEventHandler(async (event) => {
if (!["GET", "POST"].includes(event.method)) {
throw createError({
statusCode: 405,
statusMessage: "Method not allowed",
});
}
const user = await serverSupabaseUser(event);
const supabase = serverSupabaseServiceRole<Database>(event);

if (!user) throw createError({ status: 401, message: "Unauthorized" });

const { id: requesterID } = user;

const { data, error } = await supabase
.from("users")
.select("perms")
.eq("id", requesterID)
.maybeSingle();
if (error) throw createError(error);
if (!data) throw createError({ status: 404, message: "User not found" });
let { perms } = data;
if (!perms) perms = [];
if (!perms?.includes("root"))
throw createError({ status: 403, message: "Forbidden" });

switch (event.method) {
case "GET": {
const { data: invitations, error: invitationsError } =
await supabase.from("registration-links").select();
if (invitationsError) throw createError(invitationsError);
return invitations;
}
case "POST": {
const { metadata, initialPerms } = await readBody(event);
const { data: invitation, error: invitationError } = await supabase
.from("registration-links")
.insert({
metadata,
"initial-perms": initialPerms,
})
.select()
.maybeSingle();
if (invitationError) throw createError(invitationError);
if (!invitation)
throw createError({
status: 500,
message:
"Invitation was not created and an error was not thrown",
});
return invitation;
}
}
});

0 comments on commit 1d555c9

Please sign in to comment.