GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - btmorex/hapi-server-session: Simple server-side session support for hapi

BranchesTags
Open more actions menu

Latest commit

 

History

122 Commits
122 Commits

Folders and files

NameName
Last commit message
Last commit date
example
example
 
 
lib
lib
 
 
test
test
 
 
.eslintignore
.eslintignore
 
 
.eslintrc.json
.eslintrc.json
 
 
.gitignore
.gitignore
 
 
.npmrc
.npmrc
 
 
.prettierignore
.prettierignore
 
 
.prettierrc.json
.prettierrc.json
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
package.json
package.json
 
 

Repository files navigation

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

  • algorithm: [Default: 'sha256'] algorithm to use during signing
  • cache: supports the same options as server.cache(options)
    • expiresIn: [Default: session expiresIn if set or 2147483647] session cache expiration in milliseconds
    • segment: [Default: 'session'] session cache segment
  • cookie: supports the same options as server.state(name, [options])
    • isSameSite: [Default: 'Lax'] sets the SameSite flag
    • path: [Default: '/'] sets the Path flag
    • ttl: [Default: session expiresIn if set] sets the Expires and Max-Age flags
  • expiresIn: session expiration in milliseconds
  • name: [Default: 'id'] name of the cookie
  • key: signing key. Prevents weaknesses in randomness from affecting overall security
  • size: [Default: 16] number of random bytes in the session id
  • vhost: [Default: *] host or list of hosts that plugin should be enabled for

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

  • default SameSite flag to Lax. Could break sites that require session during certain kinds of cross site requests. See https://www.owasp.org/index.php/SameSite

Author

Avery Fay

About

Simple server-side session support for hapi

Resources

Readme
MIT license
Activity

Stars

22 stars

Watchers

1 watching

Forks

4 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.