Skeleton: Some login logic fixes

This commit is contained in:
2025-02-17 22:56:03 +01:00
parent 04b69611a1
commit 4a1fcc6447
4 changed files with 25 additions and 21 deletions

View File

@ -112,7 +112,9 @@ export const fetch_users = async (fetch: (_: any) => Promise<Response>): Promise
.getFullList({ fetch: fetch, sort: "+username" }); .getFullList({ fetch: fetch, sort: "+username" });
users.map((user: User) => { users.map((user: User) => {
user.avatar_url = pb.files.getURL(user, user.avatar); if (user.avatar) {
user.avatar_url = pb.files.getURL(user, user.avatar);
}
}); });
return users; return users;

View File

@ -8,6 +8,7 @@ export let pbUser: User | undefined = undefined;
const update_user = async (record: AuthRecord): Promise<void> => { const update_user = async (record: AuthRecord): Promise<void> => {
if (!record) { if (!record) {
pbUser = undefined; pbUser = undefined;
console.log("Returning with pbUser = undefined");
return; return;
} }
@ -32,8 +33,11 @@ const update_user = async (record: AuthRecord): Promise<void> => {
}; };
// Update the pbUser object when authStore changes (e.g. after logging in) // Update the pbUser object when authStore changes (e.g. after logging in)
pb.authStore.onChange(() => { pb.authStore.onChange(async () => {
update_user(pb.authStore.record); await update_user(pb.authStore.record);
// console.log("Updating pbUser...")
// console.dir(pbUser, { depth: null }); // TODO: If the user has not chosen an avatar,
// the page keeps displaying the "Login" button (wtf)
console.log("Updating pbUser...");
console.dir(pbUser, { depth: null });
}, true); }, true);

View File

@ -19,7 +19,6 @@
RaceResultCard, RaceResultCard,
} from "$lib/components"; } from "$lib/components";
import { get_avatar_preview_event_handler } from "$lib/image"; import { get_avatar_preview_event_handler } from "$lib/image";
import { import {
AppBar, AppBar,
storePopup, storePopup,
@ -39,7 +38,7 @@
getToastStore, getToastStore,
} from "@skeletonlabs/skeleton"; } from "@skeletonlabs/skeleton";
import { computePosition, autoUpdate, offset, shift, flip, arrow } from "@floating-ui/dom"; import { computePosition, autoUpdate, offset, shift, flip, arrow } from "@floating-ui/dom";
import { invalidateAll } from "$app/navigation"; import { invalidate } from "$app/navigation";
import { get_error_toast } from "$lib/toast"; import { get_error_toast } from "$lib/toast";
import { pb } from "$lib/pocketbase"; import { pb } from "$lib/pocketbase";
import { AVATAR_HEIGHT, AVATAR_WIDTH } from "$lib/config"; import { AVATAR_HEIGHT, AVATAR_WIDTH } from "$lib/config";
@ -135,8 +134,8 @@
storePopup.set({ computePosition, autoUpdate, offset, shift, flip, arrow }); storePopup.set({ computePosition, autoUpdate, offset, shift, flip, arrow });
// Reactive state // Reactive state
let username_value: string = $state(""); let username_value: string = $state(data.user?.username ?? "");
let firstname_value: string = $state(""); let firstname_value: string = $state(data.user?.firstname ?? "");
let password_value: string = $state(""); let password_value: string = $state("");
let avatar_value: FileList | undefined = $state(); let avatar_value: FileList | undefined = $state();
@ -147,15 +146,14 @@
} catch (error) { } catch (error) {
toastStore.trigger(get_error_toast("" + error)); toastStore.trigger(get_error_toast("" + error));
} }
await invalidateAll(); await invalidate("data:user");
drawerStore.close(); drawerStore.close();
password_value = ""; password_value = "";
firstname_value = data.user?.firstname ?? "";
}; };
const logout = async (): Promise<void> => { const logout = async (): Promise<void> => {
pb.authStore.clear(); pb.authStore.clear();
await invalidateAll(); await invalidate("data:user");
drawerStore.close(); drawerStore.close();
username_value = ""; username_value = "";
firstname_value = ""; firstname_value = "";
@ -172,10 +170,6 @@
toastStore.trigger(get_error_toast("Please enter your first name!")); toastStore.trigger(get_error_toast("Please enter your first name!"));
return; return;
} }
if (!password_value || password_value === "") {
toastStore.trigger(get_error_toast("Please enter a password!"));
return;
}
// Avatar handling // Avatar handling
let avatar_avif: Blob | undefined = undefined; let avatar_avif: Blob | undefined = undefined;
@ -206,16 +200,20 @@
try { try {
if (create) { if (create) {
if (!password_value || password_value === "") {
toastStore.trigger(get_error_toast("Please enter a password!"));
return;
}
await pb.collection("users").create({ await pb.collection("users").create({
username: username_value, username: username_value,
firstname: firstname_value, firstname: firstname_value,
password: password_value, password: password_value,
passwordConfirm: password_value, passwordConfirm: password_value, // lol
admin: false, admin: false,
}); });
await login(); await login();
return;
} else { } else {
if (!data.user?.id || data.user.id === "") { if (!data.user?.id || data.user.id === "") {
toastStore.trigger(get_error_toast("Invalid user id!")); toastStore.trigger(get_error_toast("Invalid user id!"));
@ -227,10 +225,10 @@
firstname: firstname_value, firstname: firstname_value,
avatar: avatar_avif, avatar: avatar_avif,
}); });
invalidateAll();
drawerStore.close(); drawerStore.close();
} }
invalidate("data:users");
} catch (error) { } catch (error) {
toastStore.trigger(get_error_toast("" + error)); toastStore.trigger(get_error_toast("" + error));
} }

View File

@ -11,7 +11,7 @@ export const ssr = false;
// It will populate the "user" attribute of each page's "data" object, // It will populate the "user" attribute of each page's "data" object,
// so each page has access to the current user (or knows if no one is signed in). // so each page has access to the current user (or knows if no one is signed in).
export const load: LayoutLoad = async ({ fetch, depends }) => { export const load: LayoutLoad = async ({ fetch, depends }) => {
depends("data:graphics"); depends("data:graphics", "data:user");
return { return {
// User information (synchronous) // User information (synchronous)