{"id":24521799,"url":"https://github.com/thegu/elysiajs-sveltekit-auth","last_synced_at":"2025-03-15T13:10:13.434Z","repository":{"id":273465000,"uuid":"919791803","full_name":"TheGU/elysiajs-sveltekit-auth","owner":"TheGU","description":"This template demonstrates how to integrate Elysia (a Bun-based backend framework) with SvelteKit. It includes authentication, database integration, and API route handling.","archived":false,"fork":false,"pushed_at":"2025-01-21T03:51:37.000Z","size":901,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-21T04:26:47.048Z","etag":null,"topics":["authentication","elysia","elysiajs","svelte","svelte5","sveltejs","sveltekit","template"],"latest_commit_sha":null,"homepage":"https://elysiajs-sveltekit-auth.vercel.app/","language":"Svelte","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/TheGU.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":"2025-01-21T02:48:29.000Z","updated_at":"2025-01-21T03:51:40.000Z","dependencies_parsed_at":"2025-01-21T04:36:59.528Z","dependency_job_id":null,"html_url":"https://github.com/TheGU/elysiajs-sveltekit-auth","commit_stats":null,"previous_names":["thegu/elysiajs-sveltekit-auth"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheGU%2Felysiajs-sveltekit-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheGU%2Felysiajs-sveltekit-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheGU%2Felysiajs-sveltekit-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheGU%2Felysiajs-sveltekit-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheGU","download_url":"https://codeload.github.com/TheGU/elysiajs-sveltekit-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243732300,"owners_count":20338839,"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","elysia","elysiajs","svelte","svelte5","sveltejs","sveltekit","template"],"created_at":"2025-01-22T03:16:37.009Z","updated_at":"2025-03-15T13:10:13.409Z","avatar_url":"https://github.com/TheGU.png","language":"Svelte","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SvelteKit with Elysia Backend Integration\n\nThis template demonstrates how to integrate Elysia (a Bun-based backend framework) with SvelteKit. It includes authentication, database integration, and API route handling.\n\n## Demo\n\nCheck out the live demo at: [https://elysiajs-sveltekit-auth.pattapongj.com/](https://elysiajs-sveltekit-auth.pattapongj.com/)\n\nTest login:\n\n- username: test\n- password: test\n\nOr you can just register your account.\n\nTest protected page should show access denied when not login \n[https://elysiajs-sveltekit-auth.pattapongj.com/profile](https://elysiajs-sveltekit-auth.pattapongj.com/profile)\n\nSwagger API for demo: [https://elysiajs-sveltekit-auth.pattapongj.com/api/swagger](https://elysiajs-sveltekit-auth.pattapongj.com/api/swagger)\n\n## Features\n\n- 🚀 SvelteKit frontend\n- ⚡ Elysia backend with Bun\n- 🔐 JWT Authentication\n- 🗃️ PostgreSQL with Drizzle ORM\n- 🌐 API client with type safety\n\n## Step-by-Step Setup\n\n### 1. Project Structure\n\nFirst, create a SvelteKit project and add necessary dependencies:\n\n```bash\n# Create new SvelteKit project\nnpm create svelte@latest my-app\ncd my-app\n\n# Install dependencies\nbun add elysia @elysiajs/swagger @elysiajs/jwt @elysiajs/eden\nbun add @node-rs/argon2 @oslojs/encoding\nbun add drizzle-orm postgres\nbun add -d drizzle-kit @types/pg\n```\n\n### 2. Backend Setup\n\nCreate the API route handler at `src/routes/api/[...slugs]/+server.ts`:\n\n```typescript\nimport { Elysia } from 'elysia';\nimport { swagger } from '@elysiajs/swagger';\n\nconst app = new Elysia({ prefix: '/api' })\n  .use(swagger())\n  .get('/hi', () =\u003e 'Hi Elysia');\n\nexport type App = typeof app;\n\n// Handle both GET and POST requests\nexport const GET = ({ request }) =\u003e app.handle(request);\nexport const POST = ({ request }) =\u003e app.handle(request);\n```\n\n### 3. Database Configuration\n\nSet up PostgreSQL with Docker and create schema:\n\n```bash\n# Docker compose for PostgreSQL\ndocker compose up -d\n\n# Run database migrations\nbun run db:push\n```\n\n### 4. Authentication System\n\nImplement authentication in `src/lib/server/auth.ts`:\n\n```typescript\nimport { Elysia, t } from 'elysia';\nimport { jwt } from '@elysiajs/jwt';\n\nexport const authRouter = new Elysia({ prefix: '/auth' })\n  .use(jwt({\n    secret: process.env.JWT_SECRET\n  }))\n  .post('/login', async ({ body, jwt }) =\u003e {\n    // Login logic\n  })\n  .post('/register', async ({ body, jwt }) =\u003e {\n    // Registration logic\n  });\n```\n\n### 5. API Client\n\nCreate a type-safe API client in `src/lib/api/client.ts`:\n\n```typescript\nimport { treaty } from '@elysiajs/eden';\nimport type { App } from '@/routes/api/[...slugs]/+server';\n\nexport const api = treaty\u003cApp\u003e(import.meta.env.PUBLIC_API_URL);\n```\n\n### 6. Environment Setup\n\nCreate `.env` file with necessary variables:\n\n```env\nDATABASE_URL=\"postgres://root:mysecretpassword@localhost:5432/svelte_elysia_auth\"\nPUBLIC_API_URL=http://localhost:5173\nPUBLIC_API_PATH=/api\nJWT_SECRET=your-secret-key-that-is-at-least-32-characters\nPUBLIC_TURNSTILE_SITE_KEY=your_site_key_here\nPRIVATE_TURNSTILE_SECRET_KEY=your_secret_key_here\n```\n\n## Development\n\n1. Start the database:\n```bash\nbun run db:start\n```\n\n2. Run migrations:\n```bash\nbun run db:push\n```\n\n3. Start development server:\n```bash\nbun run dev\n```\n\n## API Documentation\n\nOnce running, access the Swagger documentation at:\n- http://localhost:5173/api/swagger\n\n\u003e **Note**: Due to a known issue in @elysiajs/swagger v1.2.0, the Swagger UI may attempt to call incorrect URLs. A workaround has been implemented in `hooks.server.ts` to redirect `/swagger/json` to `/api/swagger/json`. If you experience any issues with Swagger documentation, ensure this hook is properly configured:\n\n```typescript\n// src/hooks.server.ts\nimport { handle } from '@sveltejs/kit';\n\nexport const handle = async ({ event, resolve }) =\u003e {\n  if (event.url.pathname === '/swagger/json') {\n    return Response.redirect('/api/swagger/json', 301);\n  }\n\n  return resolve(event);\n};\n```\n\n## Authentication Endpoints\n\n- POST `/api/auth/register` - Register new user\n- POST `/api/auth/login` - Login user\n- GET `/api/auth/me` - Get current user info\n\n## Type Safety\n\nThe project utilizes full type safety between frontend and backend through:\n- Elysia's built-in type inference\n- Eden treaty for type-safe API calls\n- TypeScript throughout the codebase\n\n## Production Deployment\n\nFor production deployment:\n\n1. Build the application:\n```bash\nbun run build\n```\n\n2. Start the production server:\n```bash\nbun run preview\n```\n\n## Learn More\n\n- [SvelteKit Documentation](https://kit.svelte.dev)\n- [Elysia Documentation](https://elysiajs.com)\n- [Drizzle ORM Documentation](https://orm.drizzle.team)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthegu%2Felysiajs-sveltekit-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthegu%2Felysiajs-sveltekit-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthegu%2Felysiajs-sveltekit-auth/lists"}