Started client side login logic

This commit is contained in:
WardPearce
2026-02-14 00:11:48 +13:00
parent c2156f2cac
commit 084ab75121
2 changed files with 40 additions and 3 deletions
+38 -1
View File
@@ -1,3 +1,5 @@
import sodium from 'libsodium-wrappers-sumo';
export type IsOwnBackend = {
builtWithBackend: boolean;
internalAuth: boolean;
@@ -16,4 +18,39 @@ export function isOwnBackend(): IsOwnBackend | null {
};
}
async function backendLogin(username: string, password: string) {}
export async function backendLogin(username: string, rawPassword: string) {
await sodium.ready;
const passwordSaltsResp = await fetch(`/api/user/${username}/public`);
if (!passwordSaltsResp.ok) return;
const passwordSalts = await passwordSaltsResp.json();
const loginHash = sodium.crypto_pwhash(
32,
rawPassword,
sodium.from_base64(passwordSalts.passwordSalt),
sodium.crypto_pwhash_OPSLIMIT_SENSITIVE,
sodium.crypto_pwhash_MEMLIMIT_SENSITIVE,
sodium.crypto_pwhash_ALG_DEFAULT
);
const loginResp = await fetch('/api/user/login', {
method: 'POST',
body: JSON.stringify({
username,
passwordHash: loginHash
})
});
if (!loginResp.ok) return;
const subscriptionRawKey = sodium.crypto_pwhash(
32,
rawPassword,
sodium.from_base64(passwordSalts.subscriptionPasswordSalt),
sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
sodium.crypto_pwhash_ALG_DEFAULT
);
}
@@ -1,7 +1,7 @@
import { getUser } from '$lib/backendOnly/user';
import { json } from '@sveltejs/kit';
export async function GET({ locals }) {
const user = await getUser(locals.userId);
export async function GET({ params }) {
const user = await getUser(params.userId);
return json(user.publicPasswordSalts);
}