@samsch/use-knex-sessions

Sets up express-session and connect-session-knex with reasonable defaults, and adds connect-flash.

Usage no npm install needed!

<script type="module">
  import samschUseKnexSessions from 'https://cdn.skypack.dev/@samsch/use-knex-sessions';
</script>

README

useKnexSessions

Sets up express-session and connect-session-knex with reasonable defaults, and adds connect-flash.

const express = require('express');
const knexFactory = require('knex');

const useKnexSessions = require('@local/use-knex-sessions');

const knex = knexFactory(require('./knexfile'));
const app = express();

const config = require('config.json'); // gitignore config file

useKnexSessions(app, knex, {
  secret: config.sessionSecret, // Must be a random string at least 16 characters
  cookie: {
    // setting secure to false for development lets you use local http
    // instead of needing https.
    secure: config.env === 'prod',
  },
});

The default options passed to express-session and connect-session-knex:

const defaults = {
  resave: false,
  saveUninitialized: false,
  store: {
    knex, // passed into useKnexSessions
    createtable: false,
  },
  cookie: {
    httpOnly: true,
    secure: true,
    sameSite: 'lax',
  },
}