Skip to content

Components

The ByteAuth Next.js SDK provides React components for displaying QR codes and handling authentication.

The main component for displaying the authentication QR code.

'use client';
import { ByteAuthQR } from '@bytefederal/byteauth-nextjs/client';
export default function LoginPage() {
return (
<ByteAuthQR
onSuccess={(user) => {
console.log('Authenticated:', user);
window.location.href = '/dashboard';
}}
/>
);
}
PropTypeDefaultDescription
mode'login' | 'register''login'Authentication mode
sizenumber200QR code size in pixels
refreshIntervalnumber30QR refresh interval (seconds)
pollIntervalnumber5Status polling interval (seconds)
showInstructionsbooleantrueShow instruction text
onSuccess(user: User) => void-Called on successful auth
onError(error: Error) => void-Called on error
onRefresh() => void-Called when QR refreshes
classNamestring-CSS class for container
'use client';
import { useState } from 'react';
import { ByteAuthQR } from '@bytefederal/byteauth-nextjs/client';
export default function LoginPage() {
const [error, setError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
return (
<div className="login-container">
<h1>Login with ByteVault</h1>
{error && (
<div className="error-message">{error}</div>
)}
<ByteAuthQR
mode="login"
size={250}
refreshInterval={30}
pollInterval={5}
showInstructions={true}
className="qr-wrapper"
onSuccess={(user) => {
setIsLoading(true);
// Redirect to dashboard
window.location.href = '/dashboard';
}}
onError={(error) => {
setError(error.message);
setIsLoading(false);
}}
onRefresh={() => {
console.log('QR code refreshed');
}}
/>
{isLoading && <p>Redirecting...</p>}
<p>
Don't have ByteVault?{' '}
<a href="https://bytefederal.com/bytevault">Download it here</a>
</p>
</div>
);
}

A button that opens a modal with the QR code.

'use client';
import { ByteAuthButton } from '@bytefederal/byteauth-nextjs/client';
export default function Navbar() {
return (
<nav>
<ByteAuthButton
mode="login"
onSuccess={(user) => {
window.location.href = '/dashboard';
}}
>
Sign In with ByteVault
</ByteAuthButton>
</nav>
);
}
PropTypeDefaultDescription
mode'login' | 'register''login'Authentication mode
childrenReact.ReactNode-Button content
onSuccess(user: User) => void-Success callback
onError(error: Error) => void-Error callback
classNamestring-Button CSS class
modalClassNamestring-Modal CSS class

Context provider for managing authentication state.

app/providers.tsx
'use client';
import { ByteAuthProvider } from '@bytefederal/byteauth-nextjs/client';
export function Providers({ children }: { children: React.ReactNode }) {
return (
<ByteAuthProvider
config={{
apiEndpoint: '/api/byteauth',
pollInterval: 5000,
}}
>
{children}
</ByteAuthProvider>
);
}

Access authentication state with the hook:

'use client';
import { useByteAuth } from '@bytefederal/byteauth-nextjs/client';
export default function UserProfile() {
const { user, isAuthenticated, isLoading, logout } = useByteAuth();
if (isLoading) {
return <div>Loading...</div>;
}
if (!isAuthenticated) {
return <div>Please log in</div>;
}
return (
<div>
<p>Public Key: {user.publicKey}</p>
<button onClick={logout}>Log Out</button>
</div>
);
}
PropertyTypeDescription
userUser | nullCurrent user
isAuthenticatedbooleanAuth status
isLoadingbooleanLoading state
errorError | nullCurrent error
login() => voidTrigger login
logout() => voidLog out user
refresh() => voidRefresh session
:root {
--byteauth-primary: #ec0401;
--byteauth-bg: #ffffff;
--byteauth-text: #1a1a1a;
--byteauth-border: #e5e5e5;
--byteauth-radius: 12px;
}
<ByteAuthQR
className="p-8 bg-white rounded-2xl shadow-xl"
/>
import styles from './Login.module.css';
<ByteAuthQR className={styles.qrContainer} />
import styled from 'styled-components';
import { ByteAuthQR } from '@bytefederal/byteauth-nextjs/client';
const StyledQR = styled(ByteAuthQR)`
padding: 2rem;
background: white;
border-radius: 16px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
`;
export default function Login() {
return <StyledQR onSuccess={() => {}} />;
}

Build your own QR component using hooks:

'use client';
import { useByteAuthQR } from '@bytefederal/byteauth-nextjs/client';
import QRCode from 'qrcode.react';
export default function CustomQRLogin() {
const {
qrData,
sessionId,
status,
error,
refresh,
timeUntilRefresh,
} = useByteAuthQR({
mode: 'login',
onSuccess: (user) => {
window.location.href = '/dashboard';
},
});
if (error) {
return (
<div className="error">
<p>{error.message}</p>
<button onClick={refresh}>Try Again</button>
</div>
);
}
if (status === 'authenticated') {
return <div className="success">Authenticated! Redirecting...</div>;
}
return (
<div className="qr-container">
{qrData ? (
<>
<QRCode
value={qrData}
size={200}
level="M"
fgColor="#000000"
bgColor="#ffffff"
/>
<p>Refreshes in {timeUntilRefresh}s</p>
</>
) : (
<div className="loading">Generating QR code...</div>
)}
</div>
);
}
Return ValueTypeDescription
qrDatastring | nullQR code data URL
sessionIdstringCurrent session ID
status'idle' | 'pending' | 'authenticated' | 'error'Current status
errorError | nullCurrent error
refresh() => voidManually refresh QR
timeUntilRefreshnumberSeconds until auto-refresh

For server-side rendered pages:

// app/login/page.tsx (Server Component)
import { LoginClient } from './login-client';
export default function LoginPage() {
return (
<main>
<h1>Login</h1>
<LoginClient />
</main>
);
}
// app/login/login-client.tsx (Client Component)
'use client';
import { ByteAuthQR } from '@bytefederal/byteauth-nextjs/client';
export function LoginClient() {
return (
<ByteAuthQR
onSuccess={(user) => {
window.location.href = '/dashboard';
}}
/>
);
}