Installation
Next.js SDK Installation
Section titled “Next.js SDK Installation”The ByteAuth Next.js SDK provides React components, API route handlers, and NextAuth.js integration for seamless passwordless authentication.
Requirements
Section titled “Requirements”- Node.js 18.x or higher
- Next.js 13.x or 14.x (App Router supported)
- React 18.x
- TypeScript (recommended)
Installation
Section titled “Installation”-
Install via npm/yarn/pnpm
Terminal window npm install @bytefederal/byteauth-nextjsTerminal window yarn add @bytefederal/byteauth-nextjsTerminal window pnpm add @bytefederal/byteauth-nextjs -
Configure Environment Variables
Create or update
.env.local:BYTEAUTH_DOMAIN=yourdomain.comBYTEAUTH_API_KEY=your_api_key_hereBYTEAUTH_WEBHOOK_SECRET=your_webhook_secret# For NextAuth.js integrationNEXTAUTH_URL=http://localhost:3000NEXTAUTH_SECRET=your_nextauth_secret -
Create API Route Handler
App Router (
app/api/byteauth/[...byteauth]/route.ts):import { ByteAuthHandler } from '@bytefederal/byteauth-nextjs';export const { GET, POST } = ByteAuthHandler();Pages Router (
pages/api/byteauth/[...byteauth].ts):import { ByteAuthHandler } from '@bytefederal/byteauth-nextjs';export default ByteAuthHandler(); -
Add Type Definitions (TypeScript)
The SDK includes TypeScript definitions. For enhanced type safety, add to
tsconfig.json:{"compilerOptions": {"types": ["@bytefederal/byteauth-nextjs"]}}
Environment Variables
Section titled “Environment Variables”| Variable | Description | Required |
|---|---|---|
BYTEAUTH_DOMAIN | Your registered domain name | Yes |
BYTEAUTH_API_KEY | API key from developer portal | Yes |
BYTEAUTH_WEBHOOK_SECRET | Secret for webhook verification | Recommended |
NEXTAUTH_URL | Your app’s base URL | Yes (for NextAuth) |
NEXTAUTH_SECRET | Random secret for session encryption | Yes (for NextAuth) |
Quick Start
Section titled “Quick Start”Add the QR component to your login page:
import { ByteAuthQR } from '@bytefederal/byteauth-nextjs/client';
export default function LoginPage() { return ( <main> <h1>Login</h1> <ByteAuthQR onSuccess={() => { window.location.href = '/dashboard'; }} /> </main> );}NextAuth.js Integration
Section titled “NextAuth.js Integration”For full session management, integrate with NextAuth.js:
-
Install NextAuth
Terminal window npm install next-auth -
Create Auth Configuration
app/api/auth/[...nextauth]/route.ts:import NextAuth from 'next-auth';import { ByteAuthProvider } from '@bytefederal/byteauth-nextjs/providers';const handler = NextAuth({providers: [ByteAuthProvider({domain: process.env.BYTEAUTH_DOMAIN!,apiKey: process.env.BYTEAUTH_API_KEY!,}),],callbacks: {async jwt({ token, user }) {if (user) {token.publicKey = user.publicKey;}return token;},async session({ session, token }) {session.user.publicKey = token.publicKey as string;return session;},},});export { handler as GET, handler as POST }; -
Wrap App with Session Provider
app/providers.tsx:'use client';import { SessionProvider } from 'next-auth/react';export function Providers({ children }: { children: React.ReactNode }) {return <SessionProvider>{children}</SessionProvider>;}app/layout.tsx:import { Providers } from './providers';export default function RootLayout({children,}: {children: React.ReactNode;}) {return (<html><body><Providers>{children}</Providers></body></html>);}
Verify Installation
Section titled “Verify Installation”Start your development server and test:
npm run devNavigate to your login page. You should see the ByteAuth QR code.