next-session
next-session copied to clipboard
filestore with next session
how to use db or filestore as a session storage as memory is not suited for production.?
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),
});
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
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.
Ok thanx , so how to use barebone Postgres as a session datastore
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).
It does not work as session is undefined
Could you post the stack trace?
`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 | `
`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
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.