5.1.2 • Published 4 years ago

hapi-server-session v5.1.2

Weekly downloads
119
License
MIT
Repository
github
Last release
4 years ago

hapi-server-session

Simple server-side session support for hapi

npm version Build Status Coverage Status Dependency Status

Install

$ npm install hapi-server-session

For typescript users:

$ npm install @types/hapi-server-session

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'),
    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

v5.1.0

  • support vhost option

v5.0.0

  • support hapi v19

v4.3.0

  • add way to destroy the session

v4.2.0

  • default cookie path to '/'

v4.1.0

  • default cookie ttl to session expiresIn
  • remove key requirement on session expiresIn

v4.0.0

  • support hapi v17

v3.0.0

Author

Avery Fay

5.1.2

4 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.0

5 years ago

4.3.2

6 years ago

4.3.1

6 years ago

4.3.0

6 years ago

4.2.2

6 years ago

4.2.1

7 years ago

4.2.0

7 years ago

4.1.0

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.0.2

7 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.0.5

8 years ago

2.0.4

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.0

9 years ago