Hooks: Load template avatar url if user didn't set one
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
import type { Graphic, User } from "$lib/schema";
|
||||||
import type { Handle } from "@sveltejs/kit";
|
import type { Handle } from "@sveltejs/kit";
|
||||||
import PocketBase from "pocketbase";
|
import PocketBase from "pocketbase";
|
||||||
|
|
||||||
@ -14,14 +15,25 @@ export const handle: Handle = async ({ event, resolve }) => {
|
|||||||
|
|
||||||
if (event.locals.pb.authStore.isValid) {
|
if (event.locals.pb.authStore.isValid) {
|
||||||
// If the authentication data is valid, we make a "user" object easily available.
|
// If the authentication data is valid, we make a "user" object easily available.
|
||||||
event.locals.user = structuredClone(event.locals.pb.authStore.model);
|
event.locals.user = structuredClone(event.locals.pb.authStore.model) as User;
|
||||||
|
|
||||||
if (event.locals.user) {
|
if (event.locals.user) {
|
||||||
|
if (event.locals.pb.authStore.model.avatar) {
|
||||||
// Fill in the avatar URL
|
// Fill in the avatar URL
|
||||||
event.locals.user.avatar_url = event.locals.pb.files.getURL(
|
event.locals.user.avatar_url = event.locals.pb.files.getURL(
|
||||||
event.locals.pb.authStore.model,
|
event.locals.pb.authStore.model,
|
||||||
event.locals.pb.authStore.model.avatar,
|
event.locals.pb.authStore.model.avatar,
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
// Fill in the driver_template URL if no avatar chosen
|
||||||
|
const driver_template: Graphic = await event.locals.pb
|
||||||
|
.collection("graphics")
|
||||||
|
.getFirstListItem('name="driver_template"');
|
||||||
|
event.locals.user.avatar_url = event.locals.pb.files.getURL(
|
||||||
|
driver_template,
|
||||||
|
driver_template.file,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Set admin status for easier access
|
// Set admin status for easier access
|
||||||
event.locals.admin = event.locals.user.admin;
|
event.locals.admin = event.locals.user.admin;
|
||||||
|
|||||||
Reference in New Issue
Block a user