javascriptnode.jskoakoa-session

Koa-session making new session per request?


I'm setting up a Vue app with Koa as the backend. I've implemented a simple login system, using koa-session for sessions. This is all fine when I access the server directly (actually navigating to my server port and performing a request), but when I use fetch to access the data from my front end port, it does not recall each fetch as the same session and creates a new session each time. This is problematic because it doesn't detect the user as being logged in. What ends up happening is that a new session key is created each time I access the API, even without refreshing the page.

Here is my Koa-session config file:

import Koa from "koa";

import session from "koa-session";

import { getCookie } from "../actions/getCookie";
import { setCookie } from "../actions/setCookie";
import { destroyCookie } from "../actions/destroyCookie";

export const config: Readonly<Partial<session.opts>> = {
    store: {
        get: getCookie,
        set: setCookie,
        destroy: destroyCookie
    },
    key: "api:sess",
    maxAge: 86400000,
    httpOnly: true,
    renew: true,
    signed: false
};

export const useSession = (app: Koa) => session(config, app);```

Solution

  • Turns out it was with my fetch. I had credentials: "same-origin" instead of credentials: "include" and it messed everything up.