{"_id":"@dodiameer/sk-stateless-session","_rev":"1-126089d7074318d0a870e26149897f36","name":"@dodiameer/sk-stateless-session","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@dodiameer/sk-stateless-session","version":"0.1.0","description":"Stateless sessions for SvelteKit","repository":{"type":"git","url":"git+https://github.com/dodiameer/sk-stateless-session.git"},"author":{"name":"Mohammed Ali Agha","email":"mtxshiftg@gmail.com","url":"https://github.com/dodiameer"},"license":"MIT","devDependencies":{"@sveltejs/adapter-auto":"next","@sveltejs/kit":"next","@types/cookie":"^0.4.1","prettier":"^2.4.1","prettier-plugin-svelte":"^2.4.0","svelte":"^3.44.0","svelte-check":"^2.2.6","svelte-preprocess":"^4.9.4","svelte2tsx":"^0.4.13","tslib":"^2.3.1","typescript":"^4.4.3"},"type":"module","dependencies":{"@hapi/iron":"^6.0.0","cookie":"^0.4.1"},"exports":{"./package.json":"./package.json",".":"./index.js"},"types":"./index.d.ts","gitHead":"ebdff52652ba785705989439c037e0fc6887b239","bugs":{"url":"https://github.com/dodiameer/sk-stateless-session/issues"},"homepage":"https://github.com/dodiameer/sk-stateless-session#readme","_id":"@dodiameer/sk-stateless-session@0.1.0","_nodeVersion":"16.13.1","_npmVersion":"8.1.2","dist":{"integrity":"sha512-6rRBldYcfMWUqCIaA3u65Ur7TAnxfAGzQWv+U1nUDQedQbNpTj7UInT+w1FxvZ5g3YW9RVaS9Jp0uQUdKhEyfQ==","shasum":"2cddf089c246dbe6d7c7dc4213be5b29bd5becdd","tarball":"https://registry.npmjs.org/@dodiameer/sk-stateless-session/-/sk-stateless-session-0.1.0.tgz","fileCount":6,"unpackedSize":14800,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh3IRoCRA9TVsSAnZWagAAFbUQAJDeb9cZ64ZRachg3c8j\n1+cLxrgNnXMvf0B9D9vnZx5MrcAlNmDrTDhygEquQY4p8IO6nvSYw8dP277N\n17iS3CsvBbDa06m48eVxtZ7JqhT74h2UihwBSytaKuyhSjH6fjCUH8/mh+kP\nIfkPGJOXazUjHtrehLLMRByLS8kLrzevAJzstRfZatEIHCQExReNY14AY3nT\nQxPIFvNcpBpkIUqNWwQ8gxEBuH4xg/vDdl55u52CCDwZeCF7ZQIcVz0pL4j6\n9ACPwrwHXgaj/fFzQDsHKp59KxOQw2c4zCxY7bMzKiLebyJGFLJc3fPi5KFV\ne2931jGML9VaK1/eCH1Ifv/X7d8wa2J3y71+nWharxiXHwzaPLFt6F0C/AAm\nrOOkKXtj4Kw1b29GCBmPI6kK6IDv6AnOard3+c8PtyBDthRIvPk5HbdUZinb\nD0cLwgx4KAQ24E0JpHgLBW4XpmBA35iDUebopoxfWWGy5OPWAmHh1coEAU+8\n0nazu7zE3sfkJxwOBRBy0bWxs7stIMNOu09NrUNCfAAiD7kwXoituLTHe19f\n+U0Zx4d+TVIubCC8dwwe6jjiPBmD/fq+G7peEHhJL4eTcI9EMmeUS/Cbmx/Z\nQ1JlihmbCpYsiK5eG1iBdAt2IN9juiC8zzB/Y7Xv+xEJHt7gfzhilHptGZkL\n9MNg\r\n=aCA+\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDm9GTKFdtxY4DmYa2UORbrnM/uaI/LM3PyzZhZwcK6sAiEApz3Jy6tbCiVj5KwOLdv1vM84TNRXUV4JcZIDIBqGnNc="}]},"_npmUser":{"name":"dodiameer","email":"mtxshiftg@gmail.com"},"directories":{},"maintainers":[{"name":"dodiameer","email":"mtxshiftg@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/sk-stateless-session_0.1.0_1641841768188_0.958021183041392"},"_hasShrinkwrap":false}},"time":{"created":"2022-01-10T19:09:28.142Z","0.1.0":"2022-01-10T19:09:28.436Z","modified":"2022-04-05T05:05:50.287Z"},"maintainers":[{"name":"dodiameer","email":"mtxshiftg@gmail.com"}],"description":"Stateless sessions for SvelteKit","homepage":"https://github.com/dodiameer/sk-stateless-session#readme","repository":{"type":"git","url":"git+https://github.com/dodiameer/sk-stateless-session.git"},"author":{"name":"Mohammed Ali Agha","email":"mtxshiftg@gmail.com","url":"https://github.com/dodiameer"},"bugs":{"url":"https://github.com/dodiameer/sk-stateless-session/issues"},"license":"MIT","readme":"# SvelteKit Stateless Session\n\nA stateless session for SvelteKit-based apps\n\n## Wait, stateless sessions?\n\nYes, you read that right! Using [`@hapi/iron`](https://hapi.dev/module/iron) for encryption, you can take JSON data, convert it to a token, and send it to a client. When a new request arrives, you can take that token, decode it, and get back the exact same JSON data you put in. It is similar to JWTs but when using this project with it, you end up with an experience similar to regular server-side session, only without the drawbacks of being stateful.\n\nThis project is glue between Iron and SvelteKit to set-up the server-side session experience, so the stateless magic is the result of the effort of the maintainers of Iron, not me.\n\n## Installation\n\nInstall `@dodiameer/sk-stateless-session` with npm, pnpm, or yarn\n\n```bash\nnpm install @dodiameer/sk-stateless-session\n```\n\n```bash\npnpm install @dodiameer/sk-stateless-session\n```\n\n```bash\nyarn add @dodiameer/sk-stateless-session\n```\n\n## Usage\n\n### General setup\n\nIn your `src/hooks.ts` / `src/hooks/index.ts`, add the following code:\n\n```ts\nimport { statelessSession } from '@dodiameer/sk-stateless-session';\n\nexport const handle = statelessSession({\n\t// See parts below for options\n});\n\nexport const getSession = ({ locals }) => {\n\treturn locals.session.data;\n};\n```\n\nIf you have other hooks, you can use the `sequence` helper from SvelteKit:\n\n```ts\nimport { sequence } from '@sveltejs/kit/hooks';\nimport { statelessSession } from '@dodiameer/sk-stateless-session';\nimport someOtherHook from './someOtherHook';\n\nexport const handle = sequence(\n\tsomeOtherHook,\n\tstatelessSession({\n\t\t// See parts below for options\n\t})\n);\n```\n\n### Cookie based\n\nAdd `getSessionIdFromCookie` to your import:\n\n```ts\n// import { statelessSession } from \"@dodiameer/sk-stateless-session\"\nimport { statelessSession, getSessionIdFromCookie } from '@dodiameer/sk-stateless-session';\n```\n\nThen set your options:\n\n```ts\nexport const handle = statelessSession({\n\t// \"custom-cookie\" is optional, you don't need it if you're not using a custom cookie name\n\tgetSessionId: getSessionIdFromCookie('custom-cookie-name'),\n\tsecret: process.env.SESSION_SECRET,\n\t// This part is also optional if you don't want to use a custom cookie name\n\tcookie: {\n\t\tname: 'custom-cookie-name'\n\t}\n});\n```\n\n### Custom setup\n\nIf you want to get the session id from a header and send new session ids through a header instead of using cookies, you can do the following:\n\n```ts\nexport const handle = statelessSession({\n\tgetSessionId: (request) => {\n\t\t// `request` is the same request you'd get in hooks. Do whatever you want with it here\n\t\t// You must return a string that's a session ID or null\n\t\treturn sessionId || null;\n\t},\n\tsecret: process.env.SESSION_SECRET,\n\t// Instead of sending a cookie, send the session id in \"x-session-id\" header (or another name you want)\n\tcustomHeader: 'x-session-id'\n});\n```\n\n**NOTE:** The session ID gets changed every time session data changes, so keep that in mind when using a custom setup to handle that in your frontend.\n\n## Options\n\nThis is copy-pasted from the code. You'll also get the descriptions in your code editor if it supports JSDoc comments\n\n```ts\ninterface MakeSessionOptions {\n\t/**\n\t * Used to get the session id from the request.\n\t *\n\t * **Note**: If you store your session id in a cookie,\n\t * your can use `getSessionIdFromCookie` provided by this package.\n\t * @example\n\t * const getSeal = (request) => {\n\t *   const authToken = request.headers.authorization.replace('Bearer ', '') || null;\n\t *   return authToken;\n\t * }\n\t */\n\tgetSessionId: (request: ServerRequest) => string;\n\t/** Session secret */\n\tsecret: string;\n\t/**\n\t * Session expiration time in milliseconds. `0` means no expiration.\n\t *\n\t * @default 0 (no expiration)\n\t */\n\texpiresIn?: number;\n\t/**\n\t * If set, will return session id as a header with the provided name,\n\t * and will NOT set a cookie. If not set, will set a cookie with the session id.\n\t */\n\tcustomHeader?: string | false;\n\t/** Cookie options */\n\tcookie?: {\n\t\t/**\n\t\t * Cookie name\n\t\t *\n\t\t * @default \"sk-stateless-session\"\n\t\t */\n\t\tname?: string;\n\t} & Pick<CookieSerializeOptions, 'maxAge' | 'path' | 'secure' | 'httpOnly'>;\n}\n```\n\n## Credit\n\n- [`svelte-kit-cookie-session`](https://github.com/pixelmund/svelte-kit-cookie-session) for inspiration for this project. My project uses a similar API to access the session data, but I added some features I wanted to use.\n- [`iron-store`](https://github.com/vvo/iron-store) for providing a low-level store/session implementation with `@hapi/iron`. I used their codebase as a reference a few times while writing this project.\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n","readmeFilename":"README.md"}