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

Inconsistent session data access #120

Closed
IvanLisz opened this issue Jan 12, 2024 · 2 comments · Fixed by #121
Closed

Inconsistent session data access #120

IvanLisz opened this issue Jan 12, 2024 · 2 comments · Fixed by #121

Comments

@IvanLisz
Copy link

  1. Session cookie is always accessible via event.request.headers.get('cookie'), but data is not event.context.sessions (used in useSession).

  2. Sometimes the session data is there, and sometimes it's undefined.
    Using a middleware to check sessions (just with an await useSession(event, sessionConfig)) makes it work the route level.


middleware:

import { createMiddleware, useSession } from "@solidjs/start/server";
import { sessionConfig } from "~/lib/session";

export default createMiddleware({
  onRequest: [
    async (event) => {
        await useSession(event, sessionConfig)
    },
  ],
});

route:

import { cache, createAsync, redirect, type RouteSectionProps } from "@solidjs/router";
import { Show } from "solid-js";
import { getRequestEvent } from "solid-js/web";
import { sessionConfig } from "~/lib/session";
import { useSession } from 'vinxi/server'

const getUser = cache(async () => {
  "use server";

  let token;
  try {

    const event = getRequestEvent()!;
    const session = await useSession(event, sessionConfig)
  
    token = session.data.token;
    console.log(Date.now(), event.request.headers.get('cookie')?.length, token, event.context.sessions)
  } catch (e){
    console.log(e);
  }

  if(!token) {
    console.log('redirect')
    throw redirect('/login')
  }
  
  return token;  
}, "token");

export const route = {
  load: getUser
};

export default function AuthenthicatedRoutes(props: RouteSectionProps) {
  const token = createAsync(getUser, { deferStream: true });

  console.log('--------------')

  return <Show when={token()}>{props.children}</Show>;
}
@nksaraf
Copy link
Owner

nksaraf commented Jan 14, 2024

I think this should be fixed by #121. I wonder if you have a way to try it and confirm that it works. Or @edivados should we add this case in our test so we can confirm its fixed.

@IvanLisz
Copy link
Author

lgtm 🚀

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

Successfully merging a pull request may close this issue.

2 participants