Next-auth: [v2] Postgres: date/time field value out of range

Created on 9 May 2020  路  5Comments  路  Source: nextauthjs/next-auth

I've added Postgres as the database adapter. When I try to log in with an OAuth provider I get the error below. The user and account records are created without errors only the session is missing.

Create session for user User {
  name: 'Sproettiemao',
  email: '[email protected]',
  image: null,
  id: 1
}
(node:2317) UnhandledPromiseRejectionWarning: QueryFailedError: date/time field value out of range: "1591612180339"
    at new QueryFailedError (/home/lori/dev/my-eui-starter/node_modules/typeorm/error/QueryFailedError.js:11:28)
    at Query.callback (/home/lori/dev/my-eui-starter/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
    at Query.handleError (/home/lori/dev/my-eui-starter/node_modules/pg/lib/query.js:146:19)
    at Connection.connectedErrorMessageHandler (/home/lori/dev/my-eui-starter/node_modules/pg/lib/client.js:236:17)
    at Connection.emit (events.js:315:20)
    at Socket.<anonymous> (/home/lori/dev/my-eui-starter/node_modules/pg/lib/connection.js:121:12)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:273:9)
    at Socket.Readable.push (_stream_readable.js:214:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
(node:2317) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
bug

All 5 comments

Thanks for raising this! That is really helpful info about what works and what doesn't, especially I haven't tried it with Postgres yet.

I'm actually expecting to run into problems with the date/time values, which is why I've left adding in session and token expiry / updating till now!

I think I'll need to fire it up with a few local adapters (I'm thinking MySQL, Postgres, MongoDB) and work out the best common date storage method to use that works well across a range of database (or see if I need to do something add a method to convert the date/time fields).

In the short term, as you have a fork already, you could try editing next-auth/src/models/session.js and commenting out the accessTokenExpires and expires fields:

import { randomBytes } from 'crypto'

export class Session {
  constructor(userId) {
    const date = new Date()
    const sessionExpiryInDays = 30
    const accessTokenExpiryInDays = 30

    this.id = randomBytes(32).toString('hex')
    this.userId = userId 
    this.accessToken = randomBytes(32).toString('hex')
    this.accessTokenExpires = date.setDate(date.getDate() + accessTokenExpiryInDays) 
    this.expires = date.setDate(date.getDate() + sessionExpiryInDays) 
  }
}

export const SessionSchema = {
  name: 'Session',
  target: Session,
  columns: {
    id: {
      type: 'varchar',
      primary: true,
      unique: true
    },
    userId: {
      type: 'int'
    },
    accessToken: {
      type: 'varchar',
      unique: true
    },
    accessTokenExpires: {
      type: 'date', // change to: type: 'varchar',
    },
    expires: {
      type: 'date' // change to: type: 'varchar',
    }
  }
}

鈥nd then doing something like npm install LoriKarikari/next-auth#next-auth-2 to install your version instead, to see if it works until I address this.

Obviously this is is dumb and wrong and not how tokens should be store, but I assume it will work if you just want to be able to get on trying it for now. :-)

If I'm lucky, the real fix will just be for it to set proper date / time values (instead of the timestamps it's currently returning for these fields) and it will just work.

Update: I just wrote a quick contributing guide if it's helpful!

I struggled for about 3 hours with the React linking issue when I first ran into it 馃檮 so I thought it might be helpful to document it somewhere.

I think this might be addressed in beta 13 which was just pushed to next-auth@beta.

If you try it out would appreciate any feedback!

Yes, it works now 馃帀. I wanted to take a look at it but didn't really have any time today. But I was thinking of going the ISOString route too.

Oh brilliant, that's great, thanks for letting me know!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

readywater picture readywater  路  3Comments

loonskai picture loonskai  路  3Comments

dmi3y picture dmi3y  路  3Comments

jimmiejackson414 picture jimmiejackson414  路  3Comments

ryanditjia picture ryanditjia  路  3Comments