{"id":18675903,"url":"https://github.com/foopis23/auth-hono","last_synced_at":"2025-04-12T02:12:25.587Z","repository":{"id":217332352,"uuid":"743635705","full_name":"foopis23/auth-hono","owner":"foopis23","description":"The unofficial Auth.js integration for Hono","archived":false,"fork":false,"pushed_at":"2024-06-21T15:04:58.000Z","size":180,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T02:12:19.441Z","etag":null,"topics":["auth","authjs","hono","oidc","outh","outh2"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@foopis23/auth-hono","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/foopis23.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-15T16:53:24.000Z","updated_at":"2024-09-26T14:26:40.000Z","dependencies_parsed_at":"2024-01-15T20:52:38.097Z","dependency_job_id":"962db276-46ee-47c1-aaa8-1322030e04db","html_url":"https://github.com/foopis23/auth-hono","commit_stats":null,"previous_names":["foopis23/auth-hono"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foopis23%2Fauth-hono","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foopis23%2Fauth-hono/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foopis23%2Fauth-hono/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/foopis23%2Fauth-hono/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/foopis23","download_url":"https://codeload.github.com/foopis23/auth-hono/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505926,"owners_count":21115354,"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":["auth","authjs","hono","oidc","outh","outh2"],"created_at":"2024-11-07T09:26:51.990Z","updated_at":"2025-04-12T02:12:25.556Z","avatar_url":"https://github.com/foopis23.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# auth-hono\n\nAuth Hono is the unofficial Hono integration for Auth.js. It providers a simple way to add authentication to your Hono app in a few lines of code.\n\n## Instal\n\n### npm\n\n```bash\nnpm install @foopis23/auth-hono\n```\n\n### yarn\n\n```bash\nyarn add @foopis23/auth-hono\n```\n\n### pnpm\n\n```bash\npnpm add @foopis23/auth-hono\n```\n\n### bun\n\n```bash\nbun install @foopis23/auth-hono\n```\n\n## Usage\n\n```ts\n// #src/index.ts\nimport { Hono } from \"hono\";\nimport {\n  HonoAuth,\n  getSession,\n  GetSessionResult,\n  AuthConfig,\n} from \"@foopis23/auth-hono\";\nimport { RequestContext } from \"./types\";\n\ntype RequestContext = {\n  Variables: {\n    session?: GetSessionResult;\n  };\n};\n\nconst app = new Hono\u003cRequestContext\u003e();\n\nconst authConfig: AuthConfig = {\n  providers: [\n    GitHub({\n      clientId: process.env.GITHUB_ID,\n      clientSecret: process.env.GITHUB_SECRET,\n    }),\n  ],\n};\napp.all(\"/api/auth/*\", HonoAuth(authConfig));\n\n// setup up your routes here...\n```\n\nDon't forget to set the AUTH_SECRET environment variable. This should be a minimum of 32 characters, random string. On UNIX systems you can use openssl rand -hex 32 or check out https://generate-secret.vercel.app/32.\n\nYou will also need to load the environment variables into your runtime environment. For example in Node.js with a package like dotenv.\n\n### Provider Configuration\n\nThe callback URL used by the [providers](https://authjs.dev/reference/core/providers) must be set to the following, unless you mount the ExpressAuth handler on a different path:\n\n### Signing in and signing out\n\nOnce your application is mounted you can sign in or out by making requests to the following [REST API endpoints](https://authjs.dev/reference/core/types#authaction) from your client-side code. NB: Make sure to include the csrfToken in the request body for all sign-in and sign-out requests.\n\n## Managing the session\n\nIf you are using Hono html/jsx, you can make the session data available to all routes via middleware as follows\n\n```ts\napp.use(\"*\", async (c, next) =\u003e {\n  const session = await getSession(c, authConfig);\n  c.set(\"session\", session);\n  return await next();\n});\n\n// Now in your route\napp.get(\"/\", (c) =\u003e {\n  const session = c.get(\"session\");\n\n  c.html(\n    \u003chtml\u003e\n      \u003cbody\u003e\n        \u003cp\u003eHello ${session?.user?.name}\u003c/p\u003e\n      \u003c/body\u003e\n    \u003c/html\u003e\n  );\n});\n```\n\n## Authorization\n\nYou can protect routes by checking for the presence of a session and then redirect to a login page if the session is not present. This can be done either at group level or by path as follows:\n\n```ts\n// #src/middleware.ts\n\nexport authenticatedUser : MiddlewareHandler\u003cRequestContext\u003e = function (c, next) {\n  const session = c.get(\"session\");\n  if (!session) {\n    c.redirect(\"/login\");\n    return;\n  }\n  return next();\n};\n```\n\n### Per Group\n\n```ts\n// #src/protected.routes.ts\n\nimport { authenticatedUser } from \"./middleware\";\nimport { RequestContext } from \"./types\";\n\nconst protectedRouter = new Hono\u003cRequestContext\u003e();\n\nprotectedRouter.use(authenticatedUser); // all routes after this will be protected\n\nprotectedRouter.get(\"/\", (c) =\u003e {\n  c.text(\"protected\");\n});\n\nexport default protectedRouter;\n```\n\n### By Path\n\n```ts\n// #src/index.ts\n\nimport { authenticatedUser } from \"./middleware\";\nimport { RequestContext } from \"./types\";\n\nconst app = new Hono\u003cRequestContext\u003e();\n\napp.use(\"/protected/*\", authenticatedUser); // all routes that have protected/* will be protected after this\n\napp.get(\"/\", (c) =\u003e {\n  c.text(\"public\");\n});\n\napp.get(\"/protected\", (c) =\u003e {\n  c.text(\"protected\");\n});\n```\n\n## See Also\n\n- [Auth.js](https://authjs.dev)\n- [Hono](https://hono.dev)\n\n## Credits\n\nThis was based on the [Express Auth](https://authjs.dev/reference/express) package by Rexford Essilfie.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoopis23%2Fauth-hono","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffoopis23%2Fauth-hono","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffoopis23%2Fauth-hono/lists"}