A SvelteKit component library with authentication, database setup, and beautiful pre-styled components. Built with Better-Auth, Drizzle ORM, and Tabler Icons.
# npm
npm install @jsbursik/magic-ui svelte @sveltejs/kit better-auth drizzle-orm postgres @tabler/icons-svelte
npm install -D drizzle-kit
# pnpm
pnpm add @jsbursik/magic-ui svelte @sveltejs/kit better-auth drizzle-orm postgres @tabler/icons-svelte
pnpm add -D drizzle-kit
# yarn
yarn add @jsbursik/magic-ui svelte @sveltejs/kit better-auth drizzle-orm postgres @tabler/icons-svelte
yarn add -D drizzle-kit
Run the CLI to set up all necessary files (hooks, layouts, auth pages, API routes):
# npm
npx @jsbursik/magic-ui
# pnpm
pnpm dlx @jsbursik/magic-ui
# yarn
yarn dlx @jsbursik/magic-ui
This creates:
drizzle.config.ts
- Drizzle Kit configurationsrc/hooks.server.ts
- Auth middleware with route protectionsrc/app.d.ts
- TypeScript definitionssrc/lib/db/schema.ts
- Database schema (extends package schema)src/routes/+layout.svelte
- Main layout with navbarsrc/routes/+layout.server.ts
- Server-side session loadingsrc/routes/api/auth/[...all]/+server.ts
- Auth API endpointssrc/lib/server/db.ts
- Database setupsrc/lib/server/auth.ts
- Auth instancesrc/routes/login/+page.svelte
- Login pagesrc/routes/signup/+page.svelte
- Signup pageCreate a .env
file:
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
PUBLIC_BASE_URL=http://localhost:5173
BETTER_AUTH_SECRET=your-secret-key-here
pnpm drizzle-kit generate
pnpm drizzle-kit migrate
pnpm dev
That's it! Visit http://localhost:5173/signup
to create an account.
Edit src/routes/+layout.svelte
to customize your navbar:
<Navbar {authClient} user={data.user}>
{#snippet publicLinks()}
<NavLink href="/">Home</NavLink>
<NavLink href="/about">About</NavLink>
{/snippet}
{#snippet authLinks()}
<NavLink href="/dashboard">Dashboard</NavLink>
<NavLink href="/profile">Profile</NavLink>
{/snippet}
</Navbar>
Edit src/hooks.server.ts
to add protected routes:
const protectedRoutes = ["/dashboard", "/profile", "/admin"];
All components are available after scaffolding. Examples:
<script>
import { Input, toast } from '@jsbursik/magic-ui';
let email = $state('');
function notify() {
toast.add({ message: 'Hello!', type: 'success', duration: 3000 });
}
</script>
<Input label="Email" type="email" bind:value={email} />
<button onclick={notify}>Notify</button>
The CLI creates src/lib/db/schema.ts
which re-exports all Better-Auth tables. Add your own tables there:
// src/lib/db/schema.ts
import { pgTable, text, timestamp } from "drizzle-orm/pg-core";
import { user } from "@jsbursik/magic-ui/server";
// Re-export Magic UI tables
export * from "@jsbursik/magic-ui/server";
// Add your custom tables
export const posts = pgTable("posts", {
id: text("id").primaryKey(),
userId: text("user_id").notNull().references(() => user.id),
title: text("title").notNull(),
content: text("content"),
createdAt: timestamp("created_at").defaultNow().notNull(),
});
After adding tables, run pnpm drizzle-kit generate
and pnpm drizzle-kit migrate
.
Override CSS variables in your own stylesheet:
:root {
--color-primary: #3b82f6;
--color-bg: #ffffff;
/* ... more variables */
}
Component | Description |
---|---|
Input |
Form input with label and validation |
Navbar |
Navigation bar with auth-aware links |
NavLink |
Navigation link with active state |
ModeToggle |
Dark/light mode toggle |
Toast |
Individual toast notification |
ToastContainer |
Toast notification container |
// Database
createDatabase(url: string): PostgresJsDatabase
// Auth
createAuth(db: PostgresJsDatabase): BetterAuth
initAuthClient(baseURL: string): AuthClient
// Toast Store
toast.add({ message: string, type: 'success' | 'error' | 'info', duration?: number })
toast.remove(id: string)
Running the CLI sets up everything you need:
drizzle.config.ts
) - Drizzle Kit configurationsrc/lib/db/schema.ts
) - Extends package schema for custom tablessrc/hooks.server.ts
) - Auth middleware and route protectionsrc/app.d.ts
) - TypeScript types for localssrc/routes/+layout.*
) - Main layout with navbar and session handlingsrc/routes/api/auth/[...all]/+server.ts
) - Better-Auth endpointssrc/lib/server/{db,auth}.ts
) - Database and auth instancessrc/routes/{login,signup}/+page.svelte
) - Ready-to-use auth flowsMIT
Contributions are welcome! Please open an issue or PR on GitHub.
Built with: