{"id":13749528,"url":"https://github.com/quorrajs/NodeSession","last_synced_at":"2025-05-09T12:33:12.794Z","repository":{"id":31525550,"uuid":"35090033","full_name":"quorrajs/NodeSession","owner":"quorrajs","description":"Session handling for NodeJS","archived":false,"fork":false,"pushed_at":"2016-07-18T15:33:13.000Z","size":55,"stargazers_count":109,"open_issues_count":3,"forks_count":11,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-13T21:49:06.568Z","etag":null,"topics":["nodejs","session"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/quorrajs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-05T09:23:06.000Z","updated_at":"2023-09-08T16:57:21.000Z","dependencies_parsed_at":"2022-09-09T13:21:57.658Z","dependency_job_id":null,"html_url":"https://github.com/quorrajs/NodeSession","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quorrajs%2FNodeSession","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quorrajs%2FNodeSession/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quorrajs%2FNodeSession/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quorrajs%2FNodeSession/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quorrajs","download_url":"https://codeload.github.com/quorrajs/NodeSession/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224594983,"owners_count":17337411,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","session"],"created_at":"2024-08-03T07:01:04.425Z","updated_at":"2024-11-16T00:30:48.277Z","avatar_url":"https://github.com/quorrajs.png","language":"JavaScript","readme":"NodeSession\n===========\n\nSince HTTP driven applications are stateless, sessions provide a way to store information about the user across requests.\nNodeSession ships with a variety of session back-ends available for use through a clean, unified API. Support for\nback-ends such as File and databases is included out of the box.\n\n[![npm version](https://badge.fury.io/js/node-session.svg)](http://badge.fury.io/js/node-session)\n[![Build Status](https://travis-ci.org/quorrajs/NodeSession.svg?branch=master)](https://travis-ci.org/quorrajs/NodeSession)\n[![Quality](https://codeclimate.com/github/quorrajs/NodeSession/badges/gpa.svg)](https://codeclimate.com/github/quorrajs/NodeSession)\n\n- [Installation](#installation)\n- [Session Usage](#session-usage)\n- [Configuration](#configuration)\n- [Flash Data](#flash-data)\n- [CSRF Token](#csrf-token)\n- [Database Sessions](#database-sessions)\n- [Session Drivers](#session-drivers)\n\n## Installation\n\nThe source is available for download from [GitHub](https://github.com/quorrajs/NodeSession). Alternatively, you\ncan install using Node Package Manager (npm):\n\n```javascript\nnpm install node-session\n```\n## Session Usage\n\n**Initialization**\n\n```javascript\nvar NodeSession = require('node-session');\n\n// init\nsession = new NodeSession({secret: 'Q3UBzdH9GEfiRCTKbi5MTPyChpzXLsTD'});\n\n// start session for an http request - response\n// this will define a session property to the request object\nsession.startSession(req, res, callback)\n\n```\n\n**Accessing sessions**\n\nThe session can be accessed via the HTTP request's session property.\n\n**Storing An Item In The Session**\n\n```javascript\nreq.session.put('key', 'value');\n```\n\n**Push A Value Onto An Array Session Value**\n\n```javascript\nreq.session.push('user.teams', 'developers');\n```\n\n**Retrieving An Item From The Session**\n\n```javascript\nvar value = req.session.get('key');\n```\n\n**Retrieving An Item Or Returning A Default Value**\n\n```javascript\nvar value = req.session.get('key', 'default');\n```\n\n**Retrieving An Item And Forgetting It**\n\n```javascript\nvar value = req.session.pull('key', 'default');\n```\n\n**Retrieving All Data From The Session**\n\n```javascript\nvar data = req.session.all();\n```\n\n**Determining If An Item Exists In The Session**\n\n```javascript\nif (req.session.has('users'))\n{\n    //\n}\n```\n\n**Removing An Item From The Session**\n\n```javascript\nreq.session.forget('key');\n```\n\n**Removing All Items From The Session**\n\n```javascript\nreq.session.flush();\n```\n\n**Regenerating The Session ID**\n\n```javascript\nreq.session.regenerate();\n```\n\n\n## Flash Data\n\nSometimes you may wish to store items in the session only for the next request. You may do so using the\n`req.session.flash` method:\n\n```javascript\nreq.session.flash('key', 'value');\n```\n\n**Reflashing The Current Flash Data For Another Request**\n\n```javascript\nreq.session.reflash();\n```\n\n**Reflashing Only A Subset Of Flash Data**\n\n```javascript\nreq.session.keep('username', 'email');\n```\n\n\n## CSRF Token\n\nBy default NodeSession generates and keeps CSRF token for your application in session.\n \n**Access CSRF token**\n\n```javascript\nreq.session.getToken()\n```\n\n**Regenerate CSRF token**\n\n```javascript\nreq.session.regenerateToken()\n```\n\n\n## configuration\n\nConfiguration options are passed during initialization of NodeSession module as an object. NodeSession supports following configuration\noptions.\n\n```javascript\n{\n    /*\n    |--------------------------------------------------------------------------\n    | Encryption secret\n    |--------------------------------------------------------------------------\n    |\n    | This secret key is used by the NodeSession to encrypt session and sign cookies\n    | etc. This should be set to a random, 32 character string, otherwise these\n    | encrypted strings will not be safe.\n    |\n    */\n\n    'secret': 'Q3UBzdH9GEfiRCTKbi5MTPyChpzXLsTD'\n\n    /*\n     |--------------------------------------------------------------------------\n     | Default Session Driver\n     |--------------------------------------------------------------------------\n     |\n     | This option controls the default session \"driver\" that will be used on\n     | requests. By default, NodeSession will use the lightweight file driver but\n     | you may specify any of the other wonderful drivers provided here.\n     |\n     | Supported: \"memory\", \"file\", \"database\"\n     |\n     */\n\n    'driver': 'file',\n\n    /*\n     |--------------------------------------------------------------------------\n     | Session Lifetime\n     |--------------------------------------------------------------------------\n     |\n     | Here you may specify the number of milli seconds that you wish the session\n     | to be allowed to remain idle before it expires. If you want them\n     | to immediately expire on the browser closing, set that option.\n     |\n     | Default lifetime value: 300000\n     | Default expireOnClose value: false\n     |\n     */\n\n    'lifetime': 300000, // 5 minutes\n\n    'expireOnClose': false,\n\n\n\n    /*\n     |--------------------------------------------------------------------------\n     | Session File Location\n     |--------------------------------------------------------------------------\n     |\n     | When using the file session driver, we need a location where session\n     | files may be stored. By default NodeSession will use the location shown here\n     | but a different location may be specified. This is only needed for\n     | file sessions.\n     |\n     */\n\n    'files': process.cwd() + '/sessions',\n\n    /*\n     |--------------------------------------------------------------------------\n     | Session Database Connection\n     |--------------------------------------------------------------------------\n     |\n     | When using the \"database\" session driver, you may specify a connection that\n     | should be used to manage these sessions.\n     |\n     | NodeSession uses Nodejs Waterline module for database interactions, hence\n     | it supports all databases supported by waterline. Before you specify a\n     | waterline adapter with your connection make sure that you have installed\n     | it in your application.\n     |\n     | For example before using sail-mong adapter\n     | Run:\n     | npm install sails-mongo\n     |\n     */\n\n    'connection': {\n        'adapter': 'sails-mongo',\n        'host': 'localhost',\n        'port': 27017,\n        'user': 'tron',\n        'password': '',\n        'database': 'tron'\n    },\n\n    /*\n     |--------------------------------------------------------------------------\n     | Session Database Table\n     |--------------------------------------------------------------------------\n     |\n     | When using the \"database\" session driver, you may specify the table we\n     | should use to manage the sessions. Of course, NodeSession uses `sessions`\n     | by default; however, you are free to change this as needed.\n     |\n     */\n\n    'table': 'sessions',\n\n    /*\n     |--------------------------------------------------------------------------\n     | Session Sweeping Lottery\n     |--------------------------------------------------------------------------\n     |\n     | Some session drivers must manually sweep their storage location to get\n     | rid of old sessions from storage. Here are the chances that it will\n     | happen on a given request. By default, the odds are 2 out of 100.\n     |\n     */\n\n    'lottery': [2, 100],\n\n    /*\n     |--------------------------------------------------------------------------\n     | Session Cookie Name\n     |--------------------------------------------------------------------------\n     |\n     | Here you may change the name of the cookie used to identify a session\n     | instance by ID. The name specified here will get used every time a\n     | new session cookie is created by the NodeSession for every driver.\n     |\n     | default: 'node_session'\n     |\n     */\n\n    'cookie': 'node_session',\n\n    /*\n     |--------------------------------------------------------------------------\n     | Session Cookie Path\n     |--------------------------------------------------------------------------\n     |\n     | The session cookie path determines the path for which the cookie will\n     | be regarded as available. Typically, this will be the root path of\n     | your application but you are free to change this when necessary.\n     |\n     | default: '/'\n     |\n     */\n\n    'path': '/',\n\n    /*\n     |--------------------------------------------------------------------------\n     | Session Cookie Domain\n     |--------------------------------------------------------------------------\n     |\n     | Here you may change the domain of the cookie used to identify a session\n     | in your application. This will determine which domains the cookie is\n     | available to in your application.\n     |\n     | default: null\n     |\n     */\n\n    'domain': null,\n\n    /*\n     |--------------------------------------------------------------------------\n     | HTTPS Only Cookies\n     |--------------------------------------------------------------------------\n     |\n     | By setting this option to true, session cookies will only be sent back\n     | to the server if the browser has a HTTPS connection. This will keep\n     | the cookie from being sent to you if it can not be done securely.\n     |\n     | default: false\n     |\n     */\n\n    'secure': false\n\n    /**\n    |-----------------------------------------------------------------------------\n    | Encrypt session\n    |-----------------------------------------------------------------------------\n    |\n    | If you need all stored session data to be encrypted, set the encrypt\n    | configuration option to true.\n    |\n    | default: false\n    |\n    */\n\n    'encrypt': false\n}\n```\n\nThe NodeSession uses the flash session key internally, so you should not add an item to the session by that name.\n\n\n## Database Sessions\n\nWhen using the database session driver, you may need to setup a table to contain the session items based on database.\nBelow is a required schema for the table:\n\n| filed        | type    | index  |\n|--------------|---------|--------|\n| id           | string  | unique |\n| payload      | string  |        |\n| lastActivity | integer |        |\n\n\n## Session Drivers\n\nThe session \"driver\" defines where session data will be stored for each request. NodeSession ships with several great\ndrivers out of the box:\n\n- memory - sessions will be stored in memory. Memory session driver is purposely not designed for a production\nenvironment. It will leak memory under most conditions, does not scale past a single process, and is meant for\ndebugging and developing.\n- file - sessions will be stored in files in a specified location.\n- database - sessions will be stored in a database.\n\n## To do\n\n- Add redis session driver\n\n## License\n\nThe NodeSession is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).\n\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquorrajs%2FNodeSession","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquorrajs%2FNodeSession","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquorrajs%2FNodeSession/lists"}