6.0.1 • Published 8 months ago

hapi-server-session-next v6.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

hapi-server-session-next

Simple server-side session support for hapi

npm version

Install

$ npm install hapi-server-session-next

Example

'use strict';

const hapi = require('@hapi/hapi');

const main = async () => {
  const server = new hapi.Server({
    host: 'localhost',
    address: '127.0.0.1',
    port: 8000,
  });

  await server.register({
    plugin: require('hapi-server-session-next'),
    options: {
      cookie: {
        isSecure: false, // never set to false in production
      },
    },
  });

  server.route({
    method: 'GET',
    path: '/',
    handler: (request, _h) => {
      request.session.views = request.session.views + 1 || 1;
      return 'Views: ' + request.session.views;
    },
  });

  await server.start();
};

main().catch(console.error);

Options

Questions

Can you explain what the expiresIn and ttl options do?

When the session expiresIn is not set, the cookie ttl is not set and the cache expiresIn is 2147483647. This creates a true session cookie, i.e. one that is deleted when the browser is closed, but will last forever otherwise. This is the default with no configuration.

When the session expiresIn is set, it defaults both the cookie ttl and the cache expiresIn to the same value. This creates a session that will last expiresIn milliseconds. Even if the cookie ttl is ignored by the browser, the server-side cache will expire.

More complex configurations are possible. For example, when the session expiresIn is set and the cookie ttl is explicitly set to null, a session will last until the browser is closed, but no longer than expiresIn milliseconds.

How do I destroy the session (e.g. to logout a user)?

delete request.session;

will unset the cookie and delete the session from the cache.

Changes

v6.0.1

  • support hapi v21
6.0.1

8 months ago

6.0.0

8 months ago