How to getUser without doing a server request on every page.

I am following the guide on https://tahazsh.com/blog/building-a-solidjs-app-from-scratch/ (expanding on the session in the https://start.solidjs.com/advanced/session) to create a authentication system in SolidJS Start. It works fine but it loads the if i want to use in a page, it dose a POST request to the backend server each time the page is loaded. How can i cache the user in the client, that also updates if the user logs out.
export function routeData() {
const user = createServerData$(async (_, { request }) => {
const user = await getUser(request);

if (!user) {
throw redirect("/auth/login");
}
return user;
});

return { user };
}
export function routeData() {
const user = createServerData$(async (_, { request }) => {
const user = await getUser(request);

if (!user) {
throw redirect("/auth/login");
}
return user;
});

return { user };
}
The get user, i though i could add it to the token, but i would have to redirect the user with the new headder to store it.
export const getUser = async (request: Request) => {
const session = await storage.getSession(request.headers.get("Cookie"));
const token = session.get("token");
if (!token) {
return null;
}

setToken(token.trim());

const result = await urqlClient().query(CURRENT_USER, {}).toPromise();

if (!result.data?.currentUser) {
return redirect("/auth/login");
}

return result.data.currentUser;
};
export const getUser = async (request: Request) => {
const session = await storage.getSession(request.headers.get("Cookie"));
const token = session.get("token");
if (!token) {
return null;
}

setToken(token.trim());

const result = await urqlClient().query(CURRENT_USER, {}).toPromise();

if (!result.data?.currentUser) {
return redirect("/auth/login");
}

return result.data.currentUser;
};
In react without SSR, it would just store the user in local storage, would that also be the right way to do here, as i guess that would cause problems with the SSR?
5 Replies
Birk Skyum
Birk Skyum2y ago
Have a look at this for some inspiration: https://github.com/zentered/auth0-solid-start It's using a createCookieSessionStorage to persist the session: https://start.solidjs.com/api/createCookieSessionStorage
GitHub
GitHub - zentered/auth0-solid-start: Auth0 authentication for Solid...
Auth0 authentication for Solid SSR. Contribute to zentered/auth0-solid-start development by creating an account on GitHub.
SolidStart Beta Docuentation
SolidStart Beta Documentation
Early release documentation and resources for SolidStart Beta
Kasper
KasperOP2y ago
I have looked into the Auth.js but it seems to have the same issue in that i actually need to do a server call to get the user each place i need it.
OrJDev
OrJDev2y ago
GitHub
Move common client types to core remove tsup createSession ho...
NOTE: It's a good idea to open an issue first to discuss potential changes. Please make sure that you are NOT opening a PR to fix a potential security vulnerability. Instead, please follow the...
OrJDev
OrJDev2y ago
Wait for this pr to get merged it has a createSession hook Which is a client side hook to get the current logged in user
Want results from more Discord servers?
Add your server