Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filestore with next session #363

Open
HosMercury opened this issue May 20, 2022 · 10 comments
Open

filestore with next session #363

HosMercury opened this issue May 20, 2022 · 10 comments

Comments

@HosMercury
Copy link

how to use db or filestore as a session storage as memory is not suited for production.?

@hoangvvo
Copy link
Owner

next-session is compatible with any stores built for express-session with a wrapper. You can try https://github.com/googleapis/nodejs-firestore-session

import nextSession from "next-session";
import { promisifyStore } from "next-session/lib/compat";
import {FirestoreStore} = from '@google-cloud/connect-firestore';

const firestoreStore = new FirestoreStore({
      dataset: new Firestore(),
      kind: 'express-sessions',
    })

const getSession = nextSession({
  store: promisifyStore(firestoreStore),
});

@HosMercury
Copy link
Author

HosMercury commented May 20, 2022

https://www.npmjs.com/package/session-file-store
this is what I need

import nextSession from 'next-session';
import fileStore from 'session-file-store';
import { promisifyStore } from 'next-session/lib/compat';

export const getSession = nextSession({
  store: promisifyStore(fileStore)
});

It does not work
also I tried connecting through prisma but no dice

import nextSession from 'next-session';
import { promisifyStore } from 'next-session/lib/compat';
import { PrismaSessionStore } from '@quixo3/prisma-session-store';
import prisma from '../lib/prisma';

const connectStore = new PrismaSessionStore(prisma, {
  checkPeriod: 2 * 60 * 1000,
  dbRecordIdIsSessionId: true,
  dbRecordIdFunction: undefined
});

const getSession = nextSession({
  store: promisifyStore(connectStore),
  cookie: {
    httpOnly: true,
    secure: process.env.NODE_ENV === 'production',
    maxAge: 2 * 7 * 24 * 60 * 60, // 2 weeks,
    path: '/',
    sameSite: 'strict'
  }
  // touchAfter: 1 * 7 * 24 * 60 * 60 // 1 week
});

export default async function session(req, res, next) {
  await getSession(req, res);
  next();
}

So Either filesystem store or Prisma connection, anyone of them is a solution. although i prefer file store then pure pg then prisma

@hoangvvo
Copy link
Owner

What does not work for you when using filestore? Do you specify a path for it to use? Also, keep in mind that some deployment options (like Vercel) does not allow you to save files.

@HosMercury
Copy link
Author

Ok thanx , so how to use barebone Postgres as a session datastore

@hoangvvo
Copy link
Owner

First you want to pick a session store. https://github.com/expressjs/session#compatible-session-stores is a good start. Then you want to take a look at this section: https://github.com/hoangvvo/next-session#compatibility-with-expressconnect-stores.

For example, let's use https://www.npmjs.com/package/connect-pg-simple.

import pg from 'pg';
import { expressSession } from "next-session/lib/compat";
import connectPgSimple from "connect-pg-simple";
const pgSession = connectPgSimple(expressSession);

const pgPool = new pg.Pool({
    // Insert pool options here
});

const getSession = nextSession({
  store: promisifyStore(new pgSession({
    pool : pgPool,
    tableName : 'user_sessions'
  }))
});
export default async function session(req, res, next) {
  await getSession(req, res);
  next();
}

If it does not work, let me know exactly what is the issue you are having (stack trace/behavior).

@HosMercury
Copy link
Author

It does not work as session is undefined

@hoangvvo
Copy link
Owner

Could you post the stack trace?

@HosMercury
Copy link
Author

HosMercury commented May 25, 2022

`Server Error
TypeError: Cannot read property 'Store' of undefined

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Source
(api)/lib/get-session.js (4:34) @ eval

  2 | import { promisifyStore } from 'next-session/lib/compat';
  3 | import connectPgSimple from 'connect-pg-simple';
> 4 | const pgSession = connectPgSimple(nextSession);
    |                                  ^
  5 | 
  6 | import pool from '../db/config';
  7 | `

@HosMercury
Copy link
Author

HosMercury commented May 25, 2022

`import nextSession from 'next-session';
import pgPool from '../db/config';

import { promisifyStore } from 'next-session/lib/compat';
import { expressSession } from 'next-session/lib/compat';

const pgSession = require('connect-pg-simple')(expressSession);

const connectStore = new pgSession({
  pool: pgPool,
  tableName: 'session'
});

export const getSession = nextSession({
  store: promisifyStore(connectStore),
  cookie: {
    maxAge: 432000
  }
});
` 

this works without errs
but the db table always empty

@hoangvvo
Copy link
Owner

Did you follow the steps in https://github.com/voxpelli/node-connect-pg-simple#connect-pg-simple, in particular, creaing a session table with their schema?

I have tested next-session with mongodb and redis and they work so I wonder if the issue is with your local settings or the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants