38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { createClient } from "@/utils/supabase/server";
|
|
import { validateIdentity } from "@/utils/security/validate_identity";
|
|
import { storeAccessKey_Cookie } from "@/utils/security/access_storage";
|
|
import { deriveKey } from "@/utils/security/encryption";
|
|
|
|
export async function login(email: string, password: string) {
|
|
const supabase = await createClient();
|
|
|
|
const { data, error } = await supabase.auth.signInWithPassword({
|
|
email,
|
|
password,
|
|
});
|
|
|
|
if (error) {
|
|
return { data, error };
|
|
}
|
|
|
|
await validateIdentity(password);
|
|
const { data: saltData, error: saltError } = await supabase
|
|
.from("users")
|
|
.select("salt")
|
|
.eq("id", (await supabase.auth.getUser()).data.user?.id)
|
|
.single();
|
|
|
|
if (saltError) {
|
|
throw saltError;
|
|
}
|
|
|
|
const salt = Uint8Array.from(atob(saltData.salt), (c) => c.charCodeAt(0));
|
|
const key = await deriveKey(password, salt);
|
|
await storeAccessKey_Cookie(key);
|
|
revalidatePath("/profile", "layout");
|
|
return { success: true };
|
|
}
|