Skip to content

Commit 944259f

Browse files
committed
add group names
1 parent 6beeccc commit 944259f

File tree

1 file changed

+20
-2
lines changed
  • packages/database/supabase/functions/create-group

1 file changed

+20
-2
lines changed

packages/database/supabase/functions/create-group/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ Deno.serve(async (req) => {
3939
);
4040
}
4141

42+
const input: {name?: string} = await req.json();
43+
const groupName = input.name;
44+
if (groupName === undefined) {
45+
return new Response("Missing group name", {
46+
status: 400,
47+
headers: { "Content-Type": "application/json" },
48+
});
49+
}
4250
// @ts-ignore Deno is not visible to the IDE
4351
const url = Deno.env.get("SUPABASE_URL");
4452
// @ts-ignore Deno is not visible to the IDE
@@ -66,21 +74,31 @@ Deno.serve(async (req) => {
6674
}
6775
)
6876
}
69-
const groupName = crypto.randomUUID();
77+
// This password is discarded; nobody is expected to ever login as a group.
7078
const password = crypto.randomUUID();
79+
const email = `${groupName}@groups.discoursegraphs.com`;
7180
const supabaseAdmin: DGSupabaseClient = createClient(url, service_key);
7281
let userResponse: UserResponse | undefined;
7382
try {
7483
userResponse = await supabaseAdmin.auth.admin.createUser({
75-
email: `${groupName}@groups.discoursegraphs.com`,
84+
email,
7685
password,
86+
role:'anon',
87+
user_metadata: {group: true},
7788
email_confirm: false, // eslint-disable-line @typescript-eslint/naming-convention
7889
});
7990
if (userResponse.error)
8091
throw userResponse.error;
8192
if (!userResponse.data.user)
8293
throw new Error("Did not create user");
8394
} catch (error) {
95+
if (error.code === 'email_exists') {
96+
return Response.json(
97+
{ msg: 'A group by this name exists' },
98+
{
99+
status: 400,
100+
});
101+
}
84102
return Response.json({ msg: 'Failed to create group user', error: error.message }, { status: 500 });
85103
}
86104
// eslint-disable-next-line @typescript-eslint/naming-convention

0 commit comments

Comments
 (0)