{"id":16226314,"url":"https://github.com/ghostdevv/sveltekit-basic-auth","last_synced_at":"2025-03-19T13:30:35.334Z","repository":{"id":42044633,"uuid":"476763012","full_name":"ghostdevv/sveltekit-basic-auth","owner":"ghostdevv","description":null,"archived":false,"fork":false,"pushed_at":"2023-11-15T22:13:21.000Z","size":204,"stargazers_count":25,"open_issues_count":4,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T18:47:25.051Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ghostdevv.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":"2022-04-01T14:55:53.000Z","updated_at":"2025-01-16T11:35:56.000Z","dependencies_parsed_at":"2024-10-27T20:34:32.739Z","dependency_job_id":"cdd0f9f4-3ee3-4efc-9728-b58d784e6e06","html_url":"https://github.com/ghostdevv/sveltekit-basic-auth","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghostdevv%2Fsveltekit-basic-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghostdevv%2Fsveltekit-basic-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghostdevv%2Fsveltekit-basic-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghostdevv%2Fsveltekit-basic-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ghostdevv","download_url":"https://codeload.github.com/ghostdevv/sveltekit-basic-auth/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243989576,"owners_count":20379648,"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":[],"created_at":"2024-10-10T12:48:43.709Z","updated_at":"2025-03-19T13:30:35.081Z","avatar_url":"https://github.com/ghostdevv.png","language":"TypeScript","readme":"# SvelteKit Basic Auth\n\n[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/ghostdevv/sveltekit-basic-auth)\n\u003c!-- [![Open in SvelteLab](https://docs.sveltelab.dev/button/dark_short.svg)](https://sveltelab.dev/github.com/ghostdevv/sveltekit-basic-auth) --\u003e\n\n## Complete Example\n\nThis SvelteKit hook password protects an app (SEE: [`src/hooks.server.ts`](./src/hooks.server.ts)). The hook checks for a valid auth token, and if it's not found, it prompts the user to login using the browsers built-in auth mechanism.\n\nA list of username password pairs are stored in an environment variable (SEE: [.env.example](./.env.example)). If you login successfully, you can get the authorised username from the page data.\n\n## Copy and Paste\n\nThis is the simplest form of the basic auth hook for you to copy and paste into your app, if you want a complete example that includes showing the logged in user see above.\n\n`src/hooks.server.ts`\n```ts\nimport type { Handle } from '@sveltejs/kit';\nimport { USERS } from '$env/static/private';\n\ninterface AuthUser {\n    username: string;\n    password: string;\n}\n\nconst users: AuthUser[] = JSON.parse(USERS);\n\nexport const handle: Handle = ({ event, resolve }) =\u003e {\n    const authorization = event.request.headers.get('Authorization');\n\n    if (!authorization || !authorization.startsWith('Basic '))\n        return new Response('Unauthorized', {\n            status: 401,\n            headers: {\n                'WWW-Authenticate': 'Basic realm=\"Protected\"',\n            },\n        });\n\n    const token = authorization.replace('Basic ', '');\n\n    const [username, password] = Buffer.from(token, 'base64')\n        .toString()\n        .split(':');\n\n    const user: AuthUser | undefined = users.find(\n        (u) =\u003e u.username === username \u0026\u0026 u.password === password,\n    );\n\n    if (!user)\n        return new Response('Unauthorized', {\n            status: 401,\n            headers: {\n                'WWW-Authenticate': 'Basic realm=\"Protected\"',\n            },\n        });\n\n    return resolve(event);\n};\n```\n\n`.env`\n```bash\nUSERS=\"[{\"username\":\"ghost\",\"password\":\"test\"}]\"\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghostdevv%2Fsveltekit-basic-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fghostdevv%2Fsveltekit-basic-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghostdevv%2Fsveltekit-basic-auth/lists"}