Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locals are always empty #10

Open
damiensedgwick opened this issue Jan 6, 2024 · 7 comments
Open

Locals are always empty #10

damiensedgwick opened this issue Jan 6, 2024 · 7 comments

Comments

@damiensedgwick
Copy link

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

@dezashibi
Copy link

I'm facing the same problem, have you been able to resolve it?

@dezashibi
Copy link

dezashibi commented Feb 23, 2024

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 🤔

@damiensedgwick
Copy link
Author

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.

@damiensedgwick
Copy link
Author

@knavels I am revisting this for another project and running into empty locals again, do you happen to have code snippet you could share?

@dezashibi
Copy link

dezashibi commented Mar 10, 2024

image

@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
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.

@damiensedgwick
Copy link
Author

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!

@nbb0738
Copy link

nbb0738 commented Dec 17, 2024

for some reason the word bio is cursed and doesn't work. if you rename the folder to something other than bio everything works as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants