Open damiensedgwick opened 6 months ago
I'm facing the same problem, have you been able to resolve it?
I could solve it, it's stupid though as I don't understand what's the problem, I first applied the same check in +page.server.ts
in edit
route folder and it worked as expected, no redirect locals
weren't empty, then I've renamed the bio
to something else like edit-bio
and it worked 🤔
Hey @knavels - I never did get it resolved now and I am a little disappointed that it was never addressed here or in the discord server when asking for help. Paid for a course, I kind of expected it to at least be supported.
@knavels I am revisting this for another project and running into empty locals again, do you happen to have code snippet you could share?
@damiensedgwick, sure dude, as I've mentioned I've renamed the bio
to edit-bio
and tested for both edit
and edit-bio
:
edit/+page.server.ts
import type { PageServerLoad } from "./$types";
import { adminDB } from "$lib/server/admin";
import { error, redirect } from "@sveltejs/kit";
export const load: PageServerLoad = async ({ locals, params }) => {
const uid = locals.userID;
if (!uid) {
throw redirect(301, "/login");
}
const userDoc = await adminDB.collection("users").doc(uid!).get();
const { username } = userDoc.data()!;
if (params.username !== username) {
throw error(401, "That username does not belong to you");
}
return {};
};
- `edit-bio/+page.server.ts`
```typescript
import type { PageServerLoad, Actions } from "./$types";
import { adminDB } from "$lib/server/admin";
import { error, fail, redirect } from "@sveltejs/kit";
export const load: PageServerLoad = async ({ locals, params }) => {
const uid = locals.userID;
if (!uid) {
throw redirect(301, "/login");
}
const userDoc = await adminDB.collection("users").doc(uid!).get();
const { username, bio } = userDoc.data()!;
if (params.username !== username) {
throw error(401, "That username does not belong to you");
}
return {
bio,
};
};
export const actions = {
default: async ({ locals, request, params }) => {
const uid = locals.userID;
const data = await request.formData();
const bio = data.get('bio') as string;
const userRef = adminDB.collection("users").doc(uid!);
const { username } = (await userRef.get()).data()!;
if (params.username !== username) {
throw error(401, "That username does not belong to you");
}
if (bio!.length > 260) {
return fail(400, { problem: "Bio must be less than 260 characters" });
}
await userRef.update({
bio,
});
},
} satisfies Actions;
hooks.server.ts
-> no specific changes but just to be mentioned
import { adminAuth } from "$lib/server/admin";
import type { Handle } from "@sveltejs/kit";
export const handle: Handle = async ({ event, resolve }) => { const sessionCookie = event.cookies.get("__session");
try {
const decodedClaims = await adminAuth.verifySessionCookie(sessionCookie!);
event.locals.userID = decodedClaims.uid;
console.log("found user id", event.locals);
} catch (e) {
event.locals.userID = null;
return resolve(event);
}
return resolve(event);
};
and at last the sveltekit version ^2.0.0 which I don't think that might be the case.
Thanks a bunch @knavels I'll give this another go and maybe have a little more luck this time! Such a weird issue to be having!
When trying to implement the server auth using the firebase admin sdk, locals are always empty. I cloned this repo down and used all of the code within this repo to test it to make sure it wasn't something i was doing and it was still broken.
Whenever I tried navigating to
user/bio
it would redirect to/login