S
SolidJS•2w ago
Ho-kage

Action triggering revalidation without cookie

I am implementing an admin site which uses BetterAuth. When implementing an action it seems it tries to fetch fresh data (my createAsync call) but it fails my admin validation because the cookie doesn't seem to be present. Not quite sure how I get around this. The isAdmin function is shown below (it gets called by functions in a "use server" context).
import { getRequestEvent } from "solid-js/web";
import { auth } from "./auth";

export async function isAdmin() {
const event = getRequestEvent();

if (!event) {
return false;
}

console.log(event.request.headers.get("cookie"));
const session = await auth.api.getSession({ headers: event.request.headers });

if (!session) {
return false;
}

if (!session.session.userId) {
return false;
}

if (session.user.role !== "admin") {
return false;
}

return true;
}
import { getRequestEvent } from "solid-js/web";
import { auth } from "./auth";

export async function isAdmin() {
const event = getRequestEvent();

if (!event) {
return false;
}

console.log(event.request.headers.get("cookie"));
const session = await auth.api.getSession({ headers: event.request.headers });

if (!session) {
return false;
}

if (!session.session.userId) {
return false;
}

if (session.user.role !== "admin") {
return false;
}

return true;
}
1 Reply
Ho-kage
Ho-kageOP•2w ago
I worked around this by disabling revalidate in the response and then manually calling revalidate on the client... Seems like a potential bug to me.
return json({ success: true, error: "" }, { revalidate: "none" });
return json({ success: true, error: "" }, { revalidate: "none" });
const onSave = () => {
setShowEditForm(false);
revalidate([getCategoryById.keyFor(params.id)]);
};
const onSave = () => {
setShowEditForm(false);
revalidate([getCategoryById.keyFor(params.id)]);
};
Just an update for anyone that is curious. I started a new project to try and replicate this issue more simply so that I could get someone to help or look into it further. The issue no longer occurred. The difference? In the latest version of solid router 'cache' is deprecated and 'query' is the replacement. This issue doesn't happen with query. So upgrading to the latest version and switching to query resolved the issue that I had. Happy days! I actually went so far as trying to read the source code for useAction to see what was going on under the hood 🥲
Want results from more Discord servers?
Add your server