HelloWorld
HelloWorld

Reputation: 11

Express session does not retrieve session data from Postgres

I've made a simple reproducible example in javascript:

import connectPgSimple from 'connect-pg-simple'
import express from 'express'
import session from 'express-session'
import {DB} from "./index.js";


const app = express()
const PGStore = connectPgSimple(session)

app.use(
    session({
      store: new PGStore({
        pgPromise: DB,
        schemaName: 'schema_name',
      }),
      secret: 'secretsecretsecretsecretsecretse',
      resave: false,
      saveUninitialized: false,
      cookie: {
        secure: true,
        sameSite: 'none',
        httpOnly: true,
      },
    }),
)


app.use(express.json())


app.listen(8000, () => {
  console.log(`api running on port: 8000`)
})

app.get('/set-session', (req, res) => {
  req.session.refreshToken = 'test-refresh-token'
  req.session.accessToken = 'test-access-token'
  req.session.save(() => {
    res.send('Session set!')
  })
})

app.get('/get-session', (req, res) => {
  res.json({
    refreshToken: req.session.refreshToken,
    accessToken: req.session.accessToken,
  })
})

And DB file:

import pgPromise from 'pg-promise'

const databaseOptions = {
  host: 'localhost',
  port: 5432,
  database: 'postgres',
  user: 'postgres',
  password: 'postgres',
  ssl: false,
}

const pgp = pgPromise()

export const DB = pgp(databaseOptions)

For some reason the refreshToken and accessToken are always undefined. I checked the DB and the row is successfully inserted on set-session. The values refreshToken and accessToken are there, they are just not retrieved when I call get-session.

What could be the problem?

I checked the DB and the row is successfully inserted on set-session. The values refreshToken and accessToken are there, they are just not retrieved when I call get-session.

Upvotes: 0

Views: 19

Answers (0)

Related Questions