hoangvvo / next-session

Simple promise-based session middleware for Next.js, micro, Express, and more
https://www.npmjs.com/package/next-session
MIT License
352 stars 24 forks source link

Example of SessionStore #371

Open kennym opened 2 years ago

kennym commented 2 years ago

Could you please provide a sample implementation of the SessionStore? I feel like this should be in the README.

rgdigital commented 1 year ago

Here's an SQLite3 file-based implementation, similar to how it works in PHP -

# Add SQLite3 session store to dependencies
yarn add connect-sqlite3
// ./lib/sqlite3-store.js
import { expressSession, promisifyStore } from "next-session/lib/compat";
var SQLiteStore = require("connect-sqlite3")(expressSession);

export default new SQLiteStore({ dir: "./.session/", table: "sessions" })
// ./lib/get-session.js
import nextSession from "next-session";
import { expressSession, promisifyStore } from "next-session/lib/compat";
import SQLiteStore from './sqlite3-store'

export const getSession = nextSession({
    name:'SESSION',
    store: promisifyStore(SQLiteStore),
    cookie: {
        httpOnly: false,
        path: '/',
        domain: process.env.SESSION_DOMAIN, // 'localhost'
        sameSite: 'Lax',
        secure: false // true in production
    },
});