SolidJSS
SolidJSโ€ข16mo agoโ€ข
9 replies
Mapo Doofus

Auth in layout?

Is it safe to do auth in layout components? For instance if I have a function like so and call it in the layout component with createAsync will it work on all page changes within that layout? Or is it like Nextjs where it should be called per page?

export const authOrRedirect = cache(async (url?: string) => {
  "use server";

  const sess = await getAuthSession();

  if (!sess.data.user) {
    return redirect(url || "/auth/login", 302);
  }

  const dbSess = await getUnexpiredSession(sess.data.id);

  if (!dbSess) {
    await logout();
    return redirect(url || "/auth/login", 302);
  }

  return sess;
}, "authRedirect");
Was this page helpful?