pixelmund / svelte-kit-cookie-session

⚒️ Encrypted "stateless" cookie sessions for SvelteKit
MIT License
184 stars 12 forks source link

Kit API Changes #39

Closed pixelmund closed 2 years ago

pixelmund commented 2 years ago

TODO

AlecAivazis commented 2 years ago

The cast to any is a little unfortunate but it's something I'd be willing to add by hand if there was no other option - maybe some documentation for the signature is enough for now?

edit: Spoke too soon :( my session store does not seem to be getting the new value when i call sync. I trimmed down my example so now I have a function that logs my users in that looks roughly like the following. After navigating to /routeA, my session does not contain the new data.

export async function authorizeUser(variables: {}) {
    // trigger the mutation to authorize the user
    var sessionData = await logIn({ variables })

    // update the session with the result
    await fetch('/auth/token', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(sessionData)
    })

    await (session as any).sync()

    goto('/routeA')
}

Here's the endpoint:

export async function POST({ request, locals }) {
    // save the tokens in the local session
    locals.session.set(await request.json())

    return {}
}
pixelmund commented 2 years ago

The cast to any is a little unfortunate but it's something I'd be willing to add by hand if there was no other option - maybe some documentation for the signature is enough for now?

edit: Spoke too soon :( my session store does not seem to be getting the new value when i call sync. I trimmed down my example so now I have a function that logs my users in that looks roughly like the following. After navigating to /routeA, my session does not contain the new data.

export async function authorizeUser(variables: {}) {
  // trigger the mutation to authorize the user
  var sessionData = await logIn({ variables })

  // update the session with the result
  await fetch('/auth/token', {
      method: 'POST',
      headers: {
          'Content-Type': 'application/json'
      },
      body: JSON.stringify(sessionData)
  })

  await (session as any).sync()

  goto('/routeA')
}

Here's the endpoint:

export async function POST({ request, locals }) {
  // save the tokens in the local session
  locals.session.set(await request.json())

  return {}
}

Oh no! Can you see a request to /__session.json when calling sync or have some kind of error in the console?

pixelmund commented 2 years ago

Ahh i think you have to await locals.session.set() in your endpoint! Since switching to webcrypto those methods are async.

AlecAivazis commented 2 years ago

Oh yep! That was it 👍

AlecAivazis commented 2 years ago

I am now running into a new error when calling session.sync(): Cannot set session store before subscribing. I haven't been able to dig into it much yet but i figured I would share while I do that in case you have any ideas

pixelmund commented 2 years ago

You'll have to use the session store somewhere in your application via $session. This error would also appear if you'd use the unmodified version and session.set or session.update. The set and update methods are getting available after the first subscription. Don't know the exact reason behind it.