@vitra-ai/nextjs-auth
A comprehensive Next.js authentication library built on top of Better Auth, providing a complete authentication solution with pre-built UI components, hooks, and utilities.
Features
- 🔐 Complete Auth Solution - Pre-built authentication flows with customizable UI
- 🎨 Beautiful UI Components - Modern, accessible components built with Radix UI
- 🔌 Plugin System - Flexible configuration for all Better Auth plugins
- 🏢 Organization Management - Multi-tenant support with roles and permissions
- 📱 Multiple Auth Methods - Email OTP, Magic Links, OAuth, Passkeys, and more
- 🎯 Type-Safe - Full TypeScript support with IntelliSense
- 🌍 Internationalization - Built-in localization support
- ⚡ Server & Client - Server-side and client-side authentication utilities
Installation
npm install @vitra-ai/nextjs-auth better-auth
bun install @vitra-ai/nextjs-auth better-auth
Quick Start
1. Create Auth Client
Create your authentication client with the plugins you need:
import { createAuthClient } from "@vitra-ai/nextjs-auth";
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_API_URL,
plugins: {
emailOTP: { enabled: true },
organization: {
enabled: true,
},
},
});
See CREATE_AUTH_CLIENT.md for full documentation.
2. Wrap Your App
import { AuthUIProvider } from '@vitra-ai/nextjs-auth';
import { authClient } from '@/lib/auth-client';
import '@vitra-ai/nextjs-auth/css';
export default function RootLayout({ children }) {
return (
<html>
<body>
<AuthUIProvider authClient={authClient}>
{children}
</AuthUIProvider>
</body>
</html>
);
}
3. Use Auth Components
import { AuthView, SignedIn, SignedOut } from '@vitra-ai/nextjs-auth';
export default function Page() {
return (
<>
<SignedOut>
<AuthView />
</SignedOut>
<SignedIn>
<h1>Welcome! You're signed in.</h1>
</SignedIn>
</>
);
}
Core Features
Authentication Client
The createAuthClient function provides a type-safe, flexible way to configure your authentication:
import { createAuthClient } from "@vitra-ai/nextjs-auth";
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_API_URL,
plugins: {
emailOTP: { enabled: true },
organization: { enabled: true },
twoFactor: { enabled: true },
passkey: { enabled: true },
},
additionalPlugins: [
],
});
📖 Full Documentation
UI Components
Pre-built, customizable components for common authentication flows:
<AuthView /> - Complete authentication flow
<SignedIn> / <SignedOut> - Conditional rendering
<OrganizationSwitcher /> - Multi-tenant organization selector
- Settings views for profile, security, organizations, and more
Hooks
Powerful hooks for authentication state and actions:
import { useAuthData, useCurrentOrganization } from '@vitra-ai/nextjs-auth/hooks';
function MyComponent() {
const { user, session } = useAuthData();
const { organization } = useCurrentOrganization();
return <div>Welcome, {user?.name}</div>;
}
Server Utilities
Server-side authentication helpers (coming soon):
import { getSession } from "@vitra-ai/nextjs-auth/server";
export async function GET() {
const session = await getSession();
if (!session) {
return new Response("Unauthorized", { status: 401 });
}
}
Documentation
Available Plugins
The library supports all Better Auth plugins with easy configuration:
- ✉️ Email OTP - Passwordless authentication with email codes
- 🏢 Organization - Multi-tenant support with roles and permissions
- 🔑 Passkey - WebAuthn/Passkey authentication
- 🔐 Two-Factor - TOTP-based 2FA
- ✨ Magic Link - Passwordless authentication via email links
- 👥 Multi-Session - Support for multiple concurrent sessions
- 🔧 API Key - API key authentication
- 👤 Anonymous - Anonymous user sessions
- 📝 Username - Username-based authentication
- 🌐 Generic OAuth - Custom OAuth providers
- 🔵 One Tap - Google One Tap sign-in
Exports
The package provides multiple export paths for different use cases:
import { AuthView, useAuthData } from "@vitra-ai/nextjs-auth";
import { useAuthData } from "@vitra-ai/nextjs-auth/hooks";
import { getSession } from "@vitra-ai/nextjs-auth/server";
import { LoginForm } from "@vitra-ai/nextjs-auth/forms";
import { Toaster } from "@vitra-ai/nextjs-auth/goodies";
import "@vitra-ai/nextjs-auth/css";
Development
bun install
bun run build
bun run dev
Contributing
This package is part of the Vitra AI monorepo. For contributions, please follow the monorepo guidelines.
License
Proprietary - Vitra AI
Author
Shikhar Singh shikhar.singh@vitra.ai
Links