From 30edfab0a3eb8f4650f68b3622f124d737720818 Mon Sep 17 00:00:00 2001 From: pascalyazid Date: Fri, 15 May 2026 18:39:21 +0200 Subject: [PATCH] Fixed registration, added per room message encryption --- utils/authentication/register.ts | 34 ++++++++++++++++++-------------- utils/security/room_crypto.ts | 6 +++--- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/utils/authentication/register.ts b/utils/authentication/register.ts index d26ec19..fa032bf 100644 --- a/utils/authentication/register.ts +++ b/utils/authentication/register.ts @@ -1,34 +1,38 @@ "use server"; -import { revalidatePath } from "next/cache"; -import { redirect } from "next/navigation"; - import { createClient } from "@/utils/supabase/server"; -export async function signup(email: string, password: string) { +export type SignupResult = + | { success: true; emailConfirmationNeeded: true } + | { success: true; emailConfirmationNeeded: false } + | { error: { message: string } }; + +export async function signup( + email: string, + password: string, +): Promise { const supabase = await createClient(); const { data, error } = await supabase.auth.signUp({ email, password }); if (error) { - return { data, error }; + return { + error: { message: "An error occured when trying to create the account." }, + }; } if ( data.user?.identities?.length === undefined || data.user.identities.length < 1 ) { - return { data, error: { message: "Email already in use" } }; + return { error: { message: "An account with this email already exists." } }; } - // TODO: add username field and insert it - const { error: insertError } = await supabase - .from("users") - .insert({ id: data.user.id, name: "anon", email: data.user.email }); - if (insertError) { - throw insertError; - } + // The auth trigger (handle_new_auth_user) automatically inserts into + // public.users when a new auth.users row is created — no manual insert needed. - revalidatePath("/", "layout"); - redirect("/"); + // If there's no session, email confirmation is required + const emailConfirmationNeeded = !data.session; + + return { success: true, emailConfirmationNeeded }; } diff --git a/utils/security/room_crypto.ts b/utils/security/room_crypto.ts index 0083d68..56a1148 100644 --- a/utils/security/room_crypto.ts +++ b/utils/security/room_crypto.ts @@ -4,7 +4,7 @@ import { privateDecrypt, constants, subtle, -} from "crypto"; +} from "node:crypto"; import { decryptPrivateKey } from "./decrypt_private_key"; import { createClient } from "@/utils/supabase/server"; @@ -88,8 +88,8 @@ export async function encryptMessageValue( // Web Crypto AES-GCM output = ciphertext || authTag (last 16 bytes) const buf = Buffer.from(encrypted); - const ciphertext = buf.slice(0, buf.length - 16).toString("base64"); - const authTag = buf.slice(buf.length - 16).toString("base64"); + const ciphertext = buf.slice(0, - 16).toString("base64"); + const authTag = buf.slice(- 16).toString("base64"); return { ciphertext,