{"id":13417831,"url":"https://github.com/nowaythatworked/auth-astro","last_synced_at":"2025-04-13T18:37:08.421Z","repository":{"id":65469125,"uuid":"592916016","full_name":"nowaythatworked/auth-astro","owner":"nowaythatworked","description":"Community maintained Astro integration of @auth/core","archived":false,"fork":false,"pushed_at":"2024-12-03T19:05:51.000Z","size":427,"stargazers_count":335,"open_issues_count":35,"forks_count":50,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-06T15:09:47.356Z","etag":null,"topics":["astro","authentication"],"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/nowaythatworked.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":"2023-01-24T20:08:16.000Z","updated_at":"2025-04-04T19:49:03.000Z","dependencies_parsed_at":"2024-01-16T20:33:19.093Z","dependency_job_id":"9a058ceb-a9b9-4b41-9bcf-b7c808f38702","html_url":"https://github.com/nowaythatworked/auth-astro","commit_stats":{"total_commits":91,"total_committers":15,"mean_commits":6.066666666666666,"dds":0.3296703296703297,"last_synced_commit":"297f4be16be42a4c152b1805cf586cdaed1cfa03"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nowaythatworked%2Fauth-astro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nowaythatworked%2Fauth-astro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nowaythatworked%2Fauth-astro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nowaythatworked%2Fauth-astro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nowaythatworked","download_url":"https://codeload.github.com/nowaythatworked/auth-astro/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248761982,"owners_count":21157656,"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":["astro","authentication"],"created_at":"2024-07-30T22:00:53.536Z","updated_at":"2025-04-13T18:37:08.391Z","avatar_url":"https://github.com/nowaythatworked.png","language":"TypeScript","funding_links":[],"categories":["What Do I Use...","TypeScript"],"sub_categories":["If I want to add authentication?"],"readme":"# Auth Astro\n\nAuth Astro is the easiest way to add Authentication to your Astro Project. It wraps the core of [Auth.js](https://authjs.dev/) into an Astro integration, which automatically adds the endpoints and handles everything else.\n\n#### Now supporting up to Astro 5\n(**disclaimer**: Please don't confuse this package with [astro-auth](https://github.com/astro-community/astro-auth))\n\n# Installation\n\nThe easiest way to get started is adding this package using the astro cli. \n\n```bash\nnpm run astro add auth-astro\n```\nThis will install the package and required peer-dependencies and add the integration to your config.\nYou can now jump to [configuration](#configuration)\n\nAlternatively, you can install the required packages on your own.\n\n```bash\nnpm install auth-astro@latest @auth/core@^0.18.6\n```\n\u003e [!NOTE]  \n\u003e If you´re using `pnpm` you must also install cookie: `pnpm i cookie`\n\n\nNext, you need to [add the integration to your astro config](https://docs.astro.build/en/guides/integrations-guide/#using-integrations) by importing it and listing it in the integrations array.\n\n## Configuration\n\nCreate your [auth configuration](https://authjs.dev/getting-started/providers/oauth-tutorial) file in the root of your project.\n\n```ts title=\"auth.config.ts\"\n// auth.config.ts\nimport GitHub from '@auth/core/providers/github'\nimport { defineConfig } from 'auth-astro'\n\nexport default defineConfig({\n\tproviders: [\n\t\tGitHub({\n\t\t\tclientId: import.meta.env.GITHUB_CLIENT_ID,\n\t\t\tclientSecret: import.meta.env.GITHUB_CLIENT_SECRET,\n\t\t}),\n\t],\n})\n```\n\nSome OAuth Providers request a callback URL be submitted alongside requesting a Client ID, and Client Secret. The callback URL used by the providers must be set to the following, unless you override the prefix field in the configuration:\n\n```\n[origin]/api/auth/callback/[provider]\n\n// example\n// http://localhost:4321/api/auth/callback/github\n```\n\n### Setup Environment Variables\n\nGenerate an auth secret by running `openssl rand -hex 32` in a local terminal or by visiting [generate-secret.vercel.app](https://generate-secret.vercel.app/32), copy the string, then set it as the `AUTH_SECRET` environment variable describe below.\n\nNext, set the `AUTH_TRUST_HOST` environment variable to `true` for hosting providers like Cloudflare Pages or Netlify.\n```sh\nAUTH_SECRET=\u003cauth-secret\u003e\nAUTH_TRUST_HOST=true\n```\n\n#### Deploying to Vercel?\nSetting `AUTH_TRUST_HOST` is not needed, as we also check for an active Vercel environment.\n\n### Requirements\n- Node version `\u003e= 17.4`\n- Astro config set to output mode `server`\n- [SSR](https://docs.astro.build/en/guides/server-side-rendering/) enabled in your Astro project\n\nResources:\n- [Enabling SSR in Your Project](https://docs.astro.build/en/guides/server-side-rendering/#enabling-ssr-in-your-project)\n- [Adding an Adapter](https://docs.astro.build/en/guides/server-side-rendering/#adding-an-adapter)\n\n# Usage\n\nYour authentication endpoints now live under `[origin]/api/auth/[operation]`. You can change the prefix in the configuration.\n\n## Accessing your configuration\n\nIn case you need to access your auth configuration, you can always import it by\n```ts\nimport authConfig from 'auth:config'\n```\n\n## Sign in \u0026 Sign out\n\nAstro Auth exposes two ways to sign in and out. Inline scripts and Astro Components.\n\n### With Inline script tags\n\nThe `signIn` and `signOut` methods can be imported dynamically in an inline script.\n\n```html\n---\n---\n\u003chtml\u003e\n\u003cbody\u003e\n  \u003cbutton id=\"login\"\u003eLogin\u003c/button\u003e\n  \u003cbutton id=\"logout\"\u003eLogout\u003c/button\u003e\n\n  \u003cscript\u003e\n    const { signIn, signOut } = await import(\"auth-astro/client\")\n    document.querySelector(\"#login\").onclick = () =\u003e signIn(\"github\")\n    document.querySelector(\"#logout\").onclick = () =\u003e signOut()\n  \u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n### With auth-astro's Components\n\nAlternatively, you can use the `SignIn` and `SignOut` button components provided by `auth-astro/components` importing them into your Astro [component's script](https://docs.astro.build/en/core-concepts/astro-components/#the-component-script) \n\n```jsx\n---\nimport { SignIn, SignOut } from 'auth-astro/components'\n---\n\u003chtml\u003e\n  \u003cbody\u003e\n    ...\n    \u003cSignIn provider=\"github\" /\u003e\n    \u003cSignOut /\u003e\n    ...\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Fetching the session\n\nYou can fetch the session in one of two ways. The `getSession` method can be used in the component script section to fetch the session.\n\n### Within the component script section\n\n```tsx title=\"src/pages/index.astro\"\n---\nimport { getSession } from 'auth-astro/server';\n\nconst session = await getSession(Astro.request)\n---\n{session ? (\n  \u003cp\u003eWelcome {session.user?.name}\u003c/p\u003e\n) : (\n  \u003cp\u003eNot logged in\u003c/p\u003e\n)}\n```\n### Within the Auth component\n\nAlternatively, you can use the `Auth` component to fetch the session using a render prop.\n\n```tsx title=\"src/pages/index.astro\"\n---\nimport type { Session } from '@auth/core/types';\nimport { Auth, SignIn, SignOut } from 'auth-astro/components';\n---\n\u003cAuth\u003e\n  {(session: Session) =\u003e \n    {session ? \n      \u003cSignOut\u003eLogout\u003c/SignOut\u003e\n    :\n      \u003cSignIn provider=\"github\"\u003eLogin\u003c/SignIn\u003e\n    }\n\n    \u003cp\u003e\n      {session ? `Logged in as ${session.user?.name}` : 'Not logged in'}\n    \u003c/p\u003e\n  }\n\u003c/Auth\u003e\n```\n\n# State of Project\n\nWe currently are waiting for the [PR](https://github.com/nextauthjs/next-auth/pull/9856) in the official [next-auth](https://github.com/nextauthjs/next-auth/) repository to be merged. Once this has happened, this package will be deprecated. \n\n# Contribution\nWaiting on the PR to be merged means, we can still add new features to the PR, so, if you miss anything feel free to open a PR or issue in this repo, and we will try to get it added to the official package.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnowaythatworked%2Fauth-astro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnowaythatworked%2Fauth-astro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnowaythatworked%2Fauth-astro/lists"}