{"id":18011252,"url":"https://github.com/octo8080x/fresh-session","last_synced_at":"2025-03-26T15:32:39.176Z","repository":{"id":45899402,"uuid":"514913808","full_name":"Octo8080X/fresh-session","owner":"Octo8080X","description":"Dead simple cookie-based session for Deno Fresh.","archived":false,"fork":false,"pushed_at":"2024-02-20T16:40:22.000Z","size":152,"stargazers_count":77,"open_issues_count":5,"forks_count":8,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-20T06:01:30.409Z","etag":null,"topics":["authentication","cookie","deno","fresh","session","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Octo8080X.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-07-17T17:41:38.000Z","updated_at":"2025-01-16T09:25:27.000Z","dependencies_parsed_at":"2023-11-22T17:29:36.602Z","dependency_job_id":"c3315502-ef4f-42b2-810f-e0029b4ddb2e","html_url":"https://github.com/Octo8080X/fresh-session","commit_stats":null,"previous_names":["xstevenyung/fresh-session"],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octo8080X%2Ffresh-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octo8080X%2Ffresh-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octo8080X%2Ffresh-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Octo8080X%2Ffresh-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Octo8080X","download_url":"https://codeload.github.com/Octo8080X/fresh-session/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245681530,"owners_count":20655215,"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":["authentication","cookie","deno","fresh","session","typescript"],"created_at":"2024-10-30T03:08:13.317Z","updated_at":"2025-03-26T15:32:38.745Z","avatar_url":"https://github.com/Octo8080X.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fresh Session 🍋\n\nDead simple cookie-based session for [Deno Fresh](https://fresh.deno.dev).\n\n## Get started\n\nFresh Session comes with a simple middleware to add at the root of your project,\nwhich will create or resolve a session from the request cookie.\n\n### Install / Import\n\nYou can import Fresh Session like so:\n\n```ts\nimport {\n  cookieSession,\n  CookieSessionStorage,\n  createCookieSessionStorage,\n  Session,\n  WithSession,\n} from \"https://deno.land/x/fresh_session@0.2.0/mod.ts\";\n```\n\n### Setup secret key\n\nFresh Session currently uses\n[iron-webcrypto](https://github.com/brc-dd/iron-webcrypto) encrypted cookie\ncontents.\n\niron-webcrypto requires a secret key to encrypt the session payload. Fresh\nSession uses the secret key from your\n[environment variable](https://deno.land/std/dotenv/load.ts) `APP_KEY`.\n\nIf you don't know how to setup environment variable locally, I wrote\n[an article about .env file in Deno Fresh](https://xstevenyung.com/blog/read-.env-file-in-deno-fresh).\n\n### Create a root middleware (`./routes/_middleware.ts`)\n\n```ts\nimport { MiddlewareHandlerContext } from \"$fresh/server.ts\";\nimport { cookieSession, WithSession } from \"fresh-session\";\n\nexport type State = {} \u0026 WithSession;\n\nconst session = cookieSession();\n\nfunction sessionHandler(req: Request, ctx: MiddlewareHandlerContext\u003cState\u003e) {\n  return session(req, ctx);\n}\nexport const handler = [sessionHandler];\n```\n\nLearn more about\n[Fresh route middleware](https://fresh.deno.dev/docs/concepts/middleware).\n\n### Interact with the session in your routes\n\nNow that the middleware is setup, it's going to handle creating/resolving\nsession based on the request cookie. So all that you need to worry about is\ninteracting with your session.\n\n```tsx\n// ./routes/dashboard.tsx\nimport { Handlers, PageProps } from \"$fresh/server.ts\";\nimport { WithSession } from \"https://deno.land/x/fresh_session@0.2.0/mod.ts\";\n\nexport type Data = { session: Record\u003cstring, string\u003e };\n\nexport const handler: Handlers\u003c\n  Data,\n  WithSession // indicate with Typescript that the session is in the `ctx.state`\n\u003e = {\n  GET(_req, ctx) {\n    // The session is accessible via the `ctx.state`\n    const { session } = ctx.state;\n\n    // Access data stored in the session\n    session.get(\"email\");\n    // Set new value in the session\n    session.set(\"email\", \"hello@deno.dev\");\n    // returns `true` if the session has a value with a specific key, else `false`\n    session.has(\"email\");\n    // clear all the session data\n    session.clear();\n    // Access all session data value as an object\n    session.data;\n    // Add flash data which will disappear after accessing it\n    session.flash(\"success\", \"Successfully flashed a message!\");\n    // Accessing the flashed data\n    // /!\\ This flashed data will disappear after accessing it one time.\n    session.flash(\"success\");\n    // Session Key Rotation only kv store and redis store.\n    // Is not work in cookie store.\n\n    // Rotate the session key. Only supported by the kv store and redis store, not the cookie store.\n    // If using the session for authentication, with a kv or redis store, it is recommended to rotate the key at login to prevent session fixation attack.\n    // The cookie store is immune from this issue.\n    session.keyRotate();\n\n    return ctx.render({\n      session: session.data, // You can pass the whole session data to the page\n    });\n  },\n};\n\nexport default function Dashboard({ data }: PageProps\u003cData\u003e) {\n  return \u003cdiv\u003eYou are logged in as {data.session.email}\u003c/div\u003e;\n}\n```\n\n## Usage Cookie Options\n\nsession value is cookie. can set the option for cookie.\n\n```ts\nimport { cookieSession } from \"fresh-session\";\n\nexport const handler = [\n  cookieSession({\n    maxAge: 30, //Session keep is 30 seconds.\n    httpOnly: true,\n  }),\n];\n```\n\n## cookie session based on Redis\n\nIn addition to storing session data in cookies, values can be stored in Redis.\n\n```ts\nimport { redisSession } from \"fresh-session/mod.ts\";\nimport { connect } from \"redis/mod.ts\";\n\nconst redis = await connect({\n  hostname: \"something redis server\",\n  port: 6379,\n});\n\nexport const handler = [redisSession(redis)];\n\n// or Customizable cookie options and Redis key prefix\n\nexport const handler = [\n  redisSession(redis, {\n    keyPrefix: \"S_\",\n    maxAge: 10,\n  }),\n];\n```\n\n## FAQ \u0026amp; Troubleshooting Errors\n\nSome common questions and troubleshooting errors.\n\n### \"TypeError: Headers are immutable.\"\n\nIf you are receiving this error, you are likely using a Response.redirect, which\nmakes the headers immutable. A workaround for this is to use the following\ninstead:\n\n```ts\nnew Response(null, {\n  status: 302,\n  headers: {\n    Location: \"your-url\",\n  },\n});\n```\n\n## Credit\n\nInitial work done by [@xstevenyung](https://github.com/xstevenyung)\n\nInspiration taken from [Oak Sessions](https://github.com/jcs224/oak_sessions) \u0026\nthanks to [@jcs224](https://github.com/jcs224) for all the insight!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focto8080x%2Ffresh-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Focto8080x%2Ffresh-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Focto8080x%2Ffresh-session/lists"}