Finnish translation

This commit is contained in:
2026-02-04 22:38:23 +02:00
parent 565337d914
commit 778e8e6b18
11 changed files with 211 additions and 61 deletions

View File

@@ -2,6 +2,8 @@ import { redirect } from "@solidjs/router";
import { useSession } from "vinxi/http";
import { getRandomValues, subtle, timingSafeEqual } from "crypto";
import { createUser, findUser } from "./db";
import type { Language } from "~/i18n";
import { getTranslations } from "~/i18n";
export interface Session {
id: number;
@@ -43,10 +45,15 @@ async function createHash(password: string) {
return `${saltHex}:${hash}`;
}
async function checkPassword(storedPassword: string, providedPassword: string) {
async function checkPassword(
storedPassword: string,
providedPassword: string,
lang: Language,
) {
const translations = getTranslations(lang);
const [storedSalt, storedHash] = storedPassword.split(":");
if (!storedSalt || !storedHash)
throw new Error("Invalid stored password format");
throw new Error(translations["errors.invalidStoredPasswordFormat"]);
const key = await subtle.deriveBits(
{
name: "PBKDF2",
@@ -66,20 +73,22 @@ async function checkPassword(storedPassword: string, providedPassword: string) {
const hash = Buffer.from(key);
const stored = Buffer.from(storedHash, "hex");
if (stored.length !== hash.length || !timingSafeEqual(stored, hash))
throw new Error("Invalid email or password");
throw new Error(translations["errors.invalidEmailOrPassword"]);
}
export async function passwordLogin(email: string, password: string) {
export async function passwordLogin(
email: string,
password: string,
lang: Language = "en",
) {
const translations = getTranslations(lang);
let user = await findUser({ email });
if (!user)
user = await createUser({
email,
password: await createHash(password),
});
else if (!user.password)
throw new Error(
"Account exists via OAuth. Sign in with your OAuth provider",
);
else await checkPassword(user.password, password);
else if (!user.password) throw new Error(translations["errors.oauthOnly"]);
else await checkPassword(user.password, password, lang);
return createSession(user);
}