kleydon / prisma-session-store

Express session store for Prisma
MIT License
116 stars 18 forks source link

TypeError since upgrade to prisma 2.29.0 #53

Closed ansemjo closed 2 years ago

ansemjo commented 2 years ago

I haven't looked into it very far yet but I've ran yarn upgrade earlier today, which updated both @prisma/client and prisma from 2.28.0 to 2.29.0. Trying to login or really just visiting any site which requires a session now results in the following error:

get(): TypeError: Cannot read property 'data' of undefined
TypeError: Cannot read property 'data' of undefined
    at PrismaSessionStore.<anonymous> (/path/to/project/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:301:74)
    at step (/path/to/project/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:61:23)
    at Object.next (/path/to/project/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:42:53)
    at fulfilled (/path/to/project/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:33:58)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Reverting these two packages to ~2.28.0 fixes the issue again.

For reference, my database model and initialization code ```prisma model Session { id String @id sid String @unique data String expiresAt DateTime } ``` ```typescript return Session({ name: SessionCookie, cookie: { maxAge: 7 * 24 * 60 * 60 * 1000, // ms, 7 days sameSite: "strict", secure: "auto", }, secret: key, resave: false, saveUninitialized: false, unset: "destroy", store: new PrismaSessionStore(prisma, { checkPeriod: 10 * 60 * 1000, // ms, 10 minutes dbRecordIdIsSessionId: true, dbRecordIdFunction: undefined, }), }); ```

It appears that it is this line that throws: https://github.com/kleydon/prisma-session-store/blob/8941f85564128bfb2cb1238aebe7670ddb1a3c55/src/lib/prisma-session-store.ts#L242

Since it tries to lookup a specific sid, I tried to clear cookies but that didn't help. Running the query manually returns null, which should properly return from the function before trying to parse anything?

> prisma.session.findUnique({ where: { sid: "something" }}).then(res => console.log(res));
Promise { <pending> }
> null
ansemjo commented 2 years ago

I've used the high art of console.log-debugging .. and indeed the session object is somehow undefined

the diff ```diff *** /tmp/tmp-priceless-euler/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js 2021-08-11 08:38:53.209000000 +0200 --- node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js 2021-08-11 15:38:11.431780377 +0200 *************** *** 283,286 **** --- 283,287 ---- var session, result; var _a; + console.log("DEBUG session.get()", sid); return __generator(this, function (_b) { switch (_b.label) { *************** *** 289,299 **** --- 290,303 ---- if (!(_b.sent())) return [2 /*return*/, callback === null || callback === void 0 ? void 0 : callback()]; + console.log("DEBUG case1: prisma.session.findUnique({ where: { sid:", sid, "} })") return [4 /*yield*/, this.prisma.session .findUnique({ where: { sid: sid }, }) + .then(res => { console.log("DEBUG prisma result:", res); return res; }) .catch(function () { return null; })]; case 2: session = _b.sent(); + console.log("DEBUG case2: session is", session); if (session === null) return [2 /*return*/, callback === null || callback === void 0 ? void 0 : callback()]; ```
DEBUG session.get() c2BzZpIeQkWayMR-W-4BDgupk0q5uuxc
DEBUG case1: prisma.session.findUnique({ where: { sid: c2BzZpIeQkWayMR-W-4BDgupk0q5uuxc } })
DEBUG case2: session is undefined
app:error TypeError: Cannot read property 'data' of undefined 
 TypeError: Cannot read property 'data' of undefined
    at PrismaSessionStore.<anonymous> (/path/to/project/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:305:74)
    at step (/path/to/project/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:61:23)
    at Object.next (/path/to/project/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:42:53)
    at fulfilled (/path/to/project/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session-store.js:33:58)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
prisma:query BEGIN; [] 0 ms
prisma:query SELECT "public"."Session"."id", "public"."Session"."sid", "public"."Session"."data", "public"."Session"."expiresAt" FROM "public"."Session" WHERE "public"."Session"."sid" = $1 LIMIT $2 OFFSET $3; ["c2BzZpIeQkWayMR-W-4BDgupk0q5uuxc",1,0] 1 ms
prisma:query ROLLBACK; [] 0 ms
GET 500 /api/login 52b 12.787ms 2021-08-11T13:33:07.384Z
Error: 
Invalid `prisma.session.delete()` invocation:

  An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.
    at cb (/path/to/project/node_modules/@prisma/client/runtime/index.js:35943:17)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Apparently it's Schrödinger's session though. If I add a .then() after the prisma query, the session object becomes null:

DEBUG session.get() c2BzZpIeQkWayMR-W-4BDgupk0q5uuxc
DEBUG case1: prisma.session.findUnique({ where: { sid: c2BzZpIeQkWayMR-W-4BDgupk0q5uuxc } })
prisma:query SELECT "public"."Session"."id", "public"."Session"."sid", "public"."Session"."data", "public"."Session"."expiresAt" FROM "public"."Session" WHERE "public"."Session"."sid" = $1 LIMIT $2 OFFSET $3; ["c2BzZpIeQkWayMR-W-4BDgupk0q5uuxc",1,0] 1 ms
DEBUG prisma result: null
DEBUG case2: session is null
GET 403 /api/login 12b 10.606ms 2021-08-11T13:38:22.191Z

Logging in still does not work though because I get a different error later on:

app:auth { id: 1, name: 'Max Mustermann', pkz: null, type: 'local' }
prisma:query BEGIN; [] 0 ms
prisma:query SELECT "public"."Session"."id" FROM "public"."Session" WHERE "public"."Session"."sid" = $1; ["84zxpnfBNyp-f64tQz2EELAQBTRfu9o9"] 1 ms
prisma:query ROLLBACK; [] 0 ms
(node:456710) UnhandledPromiseRejectionWarning: Error: 
Invalid `prisma.session.update()` invocation:

  An operation failed because it depends on one or more records that were required but not found. Record to update not found.
    at cb (/path/to/project/node_modules/@prisma/client/runtime/index.js:35943:17)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

I am a little lost .. is this possibly a prisma issue?

kleydon commented 2 years ago

Not sure... Haven't seen anything like this yet.

Internally, prisma-session-store has been using an older version of Prisma; I've just updated that, and hope the PR will complete this week.

Perhaps this will improve matters?

ansemjo commented 2 years ago

Aye. I've subscribed to its merging and will try again then. (just for reference: #55 appears to be the PR in question)

kleydon commented 2 years ago

👍

ansemjo commented 2 years ago

I noticed your PR updated prisma to 2.29.1 (I've used 2.29.0 above). Looking at the release notes, there's a link to this issue: https://github.com/prisma/prisma/issues/8718. I'd say this fits my observation of a "Schrödinger's session" above.

And indeed, simply updating to @prisma/client and prisma to 2.29.1 fixes this issue for me.

Thus I'm closing this. It appears to have been a prisma issue indeed.

kleydon commented 2 years ago

@ansemjo Glad to hear its resolved!