#Managing Sessions in SPA

1 messages · Page 1 of 1 (latest)

fiery bolt
#

I'm wondering if session management can be effectively implemented in a Single Page Application.

Is it even possible to achieve this with an approach like the one below?

export async function clientAction({ request }: Route.ClientActionArgs) {
  const session = await getSession(
    request.headers.get("Cookie")
  );

  return redirect("/login", {
    headers: {
      "Set-Cookie": await destroySession(session),
    },
  });
};
arctic flint
#

It's a little nonsensical for a SPA the way you represent it. The point of sessions is for the server to sign the cookie with a secret key only the server knows, so you can verify that the cookie wasn't tampered with. Can't have a secret key on the client, I'm afraid.

Any reason you can't put this information into localstorage or indexeddb or something?

fiery bolt
#

Thank you for the clarification, @arctic flint; that makes a lot of sense. Local storage works fine for almost all of my needs, but I'm looking to achieve something similar to session.flash for temporary data that expires after being read. Additionally, I'd like to pass some data through with just one request, which is why I was exploring this approach. If anyone has any recommendations for handling something like that efficiently, I'd love to hear them.

slender seal
#

your SPA consumes an API?

#

if you do, and your SPA and API are on the same hostname, you can make your API set the session cookie