solidjs-community/mediakit

[Auth] Errors when requestion SSR session in middleware

Closed this issue · 5 comments

Hey - First of all, thanks for maintaining this packages.

I keep running into an error when trying to get the session in a middleware.

export default createMiddleware({
  onRequest: [
    async function (ev) {
      const session = await getSession(ev.request, authOptions);
      ev.locals.user = session;
    },
  ],
});

When I load the page, I get a server error:

[auth][error] UnknownAction: Cannot parse action at /api/auth/session. Read more at https://errors.authjs.dev#unknownaction
at parseActionAndProviderId (file:///vault/dev/work/clip/node_modules/@auth/core/lib/utils/web.js:92:15)
at toInternalRequest (file:///vault/dev/work/clip/node_modules/@auth/core/lib/utils/web.js:24:40)
at Module.Auth (file:///vault/dev/work/clip/node_modules/@auth/core/index.js:67:35)
at Module.getSession (/vault/dev/work/clip/node_modules/@solid-mediakit/auth/index.js:210:48)
at __vite_ssr_exports__.default.__vite_ssr_import_0__.createMiddleware.onRequest (/vault/dev/work/clip/src/middleware.ts:11:49)
at eval (/vault/dev/work/clip/node_modules/@solidjs/start/dist/middleware/index.jsx:10:28)
at _callHandler (file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:1815:13)
at _handler (file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:1802:12)
at handler (file:///vault/dev/work/clip/node_modules/vinxi/lib/router-modes.js:278:14)
at async _callHandler (file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:1821:16)
at async file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:1962:19
at async Object.callAsync (file:///vault/dev/work/clip/node_modules/unctx/dist/index.mjs:72:16)
at async Server.toNodeHandle (file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:2249:7)
[h3] [unhandled] H3Error
at Module.getSession (/vault/dev/work/clip/node_modules/@solid-mediakit/auth/index.js:219:9)
... 6 lines matching cause stack trace ...
at async Server.toNodeHandle (file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:2249:7) {
cause: Error
at Module.getSession (/vault/dev/work/clip/node_modules/@solid-mediakit/auth/index.js:219:9)
at async __vite_ssr_exports__.default.__vite_ssr_import_0__.createMiddleware.onRequest (/vault/dev/work/clip/src/middleware.ts:11:21)
at async eval (/vault/dev/work/clip/node_modules/@solidjs/start/dist/middleware/index.jsx:10:22)
at async _callHandler (file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:1815:7)
at async _callHandler (file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:1821:16)
at async file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:1962:19
at async Object.callAsync (file:///vault/dev/work/clip/node_modules/unctx/dist/index.mjs:72:16)
at async Server.toNodeHandle (file:///vault/dev/work/clip/node_modules/h3/dist/index.mjs:2249:7),
statusCode: 500,
fatal: false,
unhandled: true,
statusMessage: undefined,
data: undefined
}

Note: When I try to get the session in onBeforeResponse the error disappears after the first page load.

FYI This is also reproducible with the official solid-start example.

Created a sample repo here: https://github.com/r0skar/solid-start-auth-issues (I cant share the secrets, but the provider works just fine - its the middleware that causes the issue)

And as requested another example using the jd-app template: https://github.com/r0skar/jd-app-auth-issue

Turns out the issue goes away when explitly setting the basePath variable in the auth config. iE.

export const authOptions = {
  basePath: "/api/auth",
} satisfies SolidAuthConfig;

Using

"@auth/core": "0.29.0",
"@solid-mediakit/auth": "2.0.11",

Yep that's expected actually the thing is create jd app sets up a variable that determines the base path hence why i asked to use it, not sure why it didn't work then but glad it does now.