40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
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 login_data = {
|
|
email: email,
|
|
password: password,
|
|
};
|
|
const { data, error } = await supabase.auth.signInWithPassword(login_data);
|
|
|
|
if (error) {
|
|
return { data, error };
|
|
} else {
|
|
await validateIdentity(password);
|
|
const { data, error } = await supabase
|
|
.from("users")
|
|
.select("salt")
|
|
.eq("id", (await supabase.auth.getUser()).data.user?.id)
|
|
.single();
|
|
|
|
if (error) {
|
|
throw error;
|
|
}
|
|
|
|
const salt = Uint8Array.from(atob(data.salt), (c) => c.charCodeAt(0));
|
|
const key = await deriveKey(password, salt);
|
|
storeAccessKey_Cookie(key);
|
|
revalidatePath("/profile", "layout");
|
|
redirect("/profile");
|
|
}
|
|
}
|