prisma-session-store icon indicating copy to clipboard operation
prisma-session-store copied to clipboard

Unable to set passport user on Microsoft Edge

Open Elsoberanold opened this issue 2 years ago • 6 comments

This code was tested on Firefox and was able to run as expected, although that was not the case with Microsoft Edge... Server Log shows the following error:

prisma:query SELECT `skedmill-db`.`Session`.`id`, `skedmill-db`.`Session`.`sid`, `skedmill-db`.`Session`.`data`, `skedmill-db`.`Session`.`expiresAt` FROM `skedmill-db`.`Session` WHERE `skedmill-db`.`Session`.`id` = ? LIMIT ? OFFSET ?
prisma:query COMMIT
{"level":30,"time":1667331147320,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-7","res":{"statusCode":302},"responseTime":604.0961999893188,"msg":"request completed"}     
prisma:query INSERT INTO `skedmill-db`.`Session` (`id`,`sid`,`data`,`expiresAt`) VALUES 
(?,?,?,?)
prisma:query ROLLBACK
set(): Error: 
Invalid `this.prisma[this.sessionModelName].create()` invocation in
C:\Users\gfbat\Projects\skedmill-project\node_modules\@quixo3\prisma-session-store\dist\lib\prisma-session-store.js:531:85

  528 case 4:
  529     _a.sent();
  530     return [3 /*break*/, 7];
→ 531 case 5: return [4 /*yield*/, this.prisma[this.sessionModelName].create(
Unique constraint failed on the constraint: 
`Session_sid_key`
{"level":50,"time":1667331147854,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-7","req":{"method":"GET","url":"/api/auth/callback/google?code=4%2F0ARtbsJpFn3xOPQjnClJXTmjasy3fQnnEgzgYpwlYPXWdUyflqoCCrMpKFcqq3Vhyiq2UIQ&scope=email+profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&authuser=0&prompt=none","hostname":"localhost:4000","remoteAddress":"::1","remotePort":65378},"res":{"statusCode":500},"err":{"type":"PrismaClientKnownRequestError","message":"\nInvalid `this.prisma[this.sessionModelName].create()` invocation in\nC:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@quixo3\\prisma-session-store\\dist\\lib\\prisma-session-store.js:531:85\n\n  528 case 4:\n  529     _a.sent();\n  530 
    return [3 /*break*/, 7];\n 531 case 5: return [4 /*yield*/, this.prisma[this.sessionModelName].create(\nUnique constraint failed on the constraint: `Session_sid_key`","stack":"Error: \nInvalid `this.prisma[this.sessionModelName].create()` invocation in\nC:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@quixo3\\prisma-session-store\\dist\\lib\\prisma-session-store.js:531:85\n\n  528 case 4:\n  529     _a.sent();\n  530 
    return [3 /*break*/, 7];\n 531 case 5: return [4 /*yield*/, this.prisma[this.sessionModelName].create(\nUnique constraint failed on the constraint: `Session_sid_key`\n  
  at RequestHandler.handleRequestError (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@prisma\\client\\runtime\\index.js:30873:13)\n    at RequestHandler.request 
(C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@prisma\\client\\runtime\\index.js:30856:12)\n    at PrismaClient._request (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@prisma\\client\\runtime\\index.js:31836:16)","code":"P2002","clientVersion":"4.5.0","meta":{"target":"Session_sid_key"}},"msg":"\nInvalid `this.prisma[this.sessionModelName].create()` invocation in\nC:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@quixo3\\prisma-session-store\\dist\\lib\\prisma-session-store.js:531:85\n\n  528 case 4:\n  529     _a.sent();\n  
530     return [3 /*break*/, 7];\n 531 case 5: return [4 /*yield*/, this.prisma[this.sessionModelName].create(\nUnique constraint failed on the constraint: `Session_sid_key`"}
{"level":40,"time":1667331147899,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-7","err":{"type":"FastifyError","message":"Reply was already sent.","stack":"FastifyError: Reply was already sent.\n    at _Reply.Reply.send (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\fastify\\lib\\reply.js:127:26)\n    at defaultErrorHandler (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\fastify\\lib\\error-handler.js:91:9)\n    at handleError (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\fastify\\lib\\error-handler.js:64:18)\n    at onErrorHook (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\fastify\\lib\\reply.js:699:5)\n    at wrapOnSendEnd (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\fastify\\lib\\reply.js:535:5)\n    at next (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\fastify\\lib\\hooks.js:204:7)\n    at C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@fastify\\session\\lib\\fastifySession.js:181:11\n    
at C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@fastify\\session\\lib\\session.js:171:9\n    at Immediate._onImmediate (C:\\Users\\gfbat\\Projects\\skedmill-project\\node_modules\\@quixo3\\prisma-session-store\\dist\\lib\\utils\\defer.js:16:18)\n  
  at processImmediate (node:internal/timers:466:21)","name":"FastifyError","code":"FST_ERR_REP_ALREADY_SENT","statusCode":500},"msg":"Reply already sent"}
{"level":30,"time":1667331148320,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-8","req":{"method":"OPTIONS","url":"/graphql","hostname":"localhost:4000","remoteAddress":"::1","remotePort":65395},"msg":"incoming request"}
check 2022-11-02T19:32:28.329Z
{"level":30,"time":1667331148325,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-8","res":{"statusCode":204},"responseTime":4.168399810791016,"msg":"request completed"}     
{"level":30,"time":1667331148328,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-9","req":{"method":"POST","url":"/graphql","hostname":"localhost:4000","remoteAddress":"::1","remotePort":65378},"msg":"incoming request"}
{"level":30,"time":1667331148342,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-9","res":{"statusCode":200},"responseTime":13.917999982833862,"msg":"request completed"}    
{"level":30,"time":1667331148358,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-a","req":{"method":"POST","url":"/graphql","hostname":"localhost:4000","remoteAddress":"::1","remotePort":65378},"msg":"incoming request"}
prisma:query SELECT `skedmill-db`.`Profile`.`id`, `skedmill-db`.`Profile`.`createdAt`, `skedmill-db`.`Profile`.`updatedAt`, `skedmill-db`.`Profile`.`biography`, `skedmill-db`.`Profile`.`pseudonym`, `skedmill-db`.`Profile`.`mood`, `skedmill-db`.`Profile`.`userId` FROM `skedmill-db`.`Profile` WHERE `skedmill-db`.`Profile`.`pseudonym` = ? LIMIT ? OFFSET ?
prisma:query SELECT `skedmill-db`.`User`.`id`, `skedmill-db`.`User`.`name`, `skedmill-db`.`User`.`email`, `skedmill-db`.`User`.`emailVerified`, `skedmill-db`.`User`.`image`, `skedmill-db`.`User`.`createdAt` FROM `skedmill-db`.`User` WHERE (`skedmill-db`.`User`.`id` = ? AND 1=1) LIMIT ? OFFSET ?
{ user: null, expires: 2022-11-02T19:32:28.359Z }
prisma:query SELECT `skedmill-db`.`User`.`id`, `skedmill-db`.`User`.`name`, `skedmill-db`.`User`.`email`, `skedmill-db`.`User`.`emailVerified`, `skedmill-db`.`User`.`image`, `skedmill-db`.`User`.`createdAt` FROM `skedmill-db`.`User` WHERE (`skedmill-db`.`User`.`id` = ? AND 1=1) LIMIT ? OFFSET ?
prisma:query SELECT `skedmill-db`.`Profile`.`id`, `skedmill-db`.`Profile`.`createdAt`, `skedmill-db`.`Profile`.`updatedAt`, `skedmill-db`.`Profile`.`biography`, `skedmill-db`.`Profile`.`pseudonym`, `skedmill-db`.`Profile`.`mood`, `skedmill-db`.`Profile`.`userId` FROM `skedmill-db`.`Profile` WHERE `skedmill-db`.`Profile`.`userId` IN (?)
{"level":30,"time":1667331148470,"pid":11324,"hostname":"GFBatista-PC","reqId":"req-a","res":{"statusCode":200},"responseTime":111.06369996070862,"msg":"request completed"}

Prisma schema:

model Session {
    id        String   @id @default(cuid())
    sid       String   @unique
    data      String   @db.Text
    //userId       String
    expiresAt DateTime
    //user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

For this project I've used fastify with @fastify/session plugin

app.register(fastifySession, {
            cookieName: 'user-session',
            secret: 'mysecretmysecretmysecretmysecret',//fs.readFileSync(path.join(__dirname, './../secret-key')),
            resave: false,
            saveUninitialized: false,
            cookie: {
                secure: false,
                //httpOnly: false,
                //path: '/',
                maxAge: 1000 * 60 * 60 * 24 * 1,
                sameSite: 'None'
            },
            store: new PrismaSessionStore(
                prisma,
                {
                    checkPeriod: 2 * 60 * 1000,
                    dbRecordIdIsSessionId: true,
                    //dispose: ,
                    dbRecordIdFunction: undefined
                }
            )
        })

I have checked db with prisma studio and session rows are being created on session table, although passport user is set to null on req.user.

Packages versions: "prisma": "^4.5.0", "@prisma/client": "^4.5.0", "fastify": "^4.9.2", "@fastify/session": "^10.0.2", "@fastify/cookie": "^8.3.0", "@fastify/passport": "^2.2.0", "@quixo3/prisma-session-store": "^3.1.10"

Elsoberanold avatar Nov 01 '22 19:11 Elsoberanold

@Elsoberanold - Thanks for reporting this. Haven't used Edge or Fastify, so not immediately sure what's going on here... If the problem persists perhaps you could post a minimal reproduction? I'm a bit short on time over the near term, but hopefully others can jump in, given an example to test from.

kleydon avatar Nov 04 '22 01:11 kleydon

@Elsoberanold - Thanks for reporting this. Haven't used Edge or Fastify, so not immediately sure what's going on here... If the problem persists perhaps you could post a minimal reproduction? I'm a bit short on time over the near term, but hopefully others can jump in, given an example to test from.

This weekend I will try to post a small example to reproduce this error!

Elsoberanold avatar Nov 04 '22 04:11 Elsoberanold

@kleydon I have made an example app that reproduces this error.

https://github.com/Elsoberanold/fastify-passport-session-example

Elsoberanold avatar Nov 05 '22 11:11 Elsoberanold

Thanks @Elsoberanold.

Any prisma-session-store users out there using fastify + passport session, who might have a quick insight on this?

kleydon avatar Nov 05 '22 18:11 kleydon

mongodb + prisma + passport + nodejs + express

 '  540 case 4:\n' +
    '  541     _a.sent();\n' +
    '  542     return [3 /*break*/, 7];\n' +
    '→ 543 case 5: return [4 /*yield*/, this.prisma[this.sessionModelName].create(\n' +
    `Inconsistent column data: Malformed ObjectID: invalid character 'k' was found at index 0 in the provided hex string: "kUvJl1mpDqEYFkN9X67aCp5mwT-X3TSn" for the field 'id'.`,

I encountered the same error, but I managed to resolve it by modifying the session model.

model Session {
  id        String   @id @map("_id")
  sid       String   @unique
  data      String
  expiresAt DateTime
}

The issue was likely primarily caused by the definition of the id field. In my case, I used id String @id @default(auto()) @map("_id") @db.ObjectId which resulted in the error. After changing it to the definition mentioned above, the error disappeared, and the session could be successfully stored in the database.

Rabithua avatar Jan 31 '24 04:01 Rabithua

@Rabithua - Thanks for posting this fix.

kleydon avatar Jan 31 '24 15:01 kleydon