Get ZKAuth running in your application in under 5 minutes with this step-by-step guide.
Follow these simple steps to integrate ZKAuth into your application
Add ZKAuth to your project
npm install @zkauth/sdkSet up the ZKAuth client
import { ZKAuth } from '@zkauth/sdk';
const zkauth = new ZKAuth({
apiKey: 'zka_...',
baseUrl: 'https://api.zkauth.com'
});Register a new user with ZK proof
// Register a new user
const user = await zkauth.signUp(
'user@example.com',
'secure-password'
);
console.log('User created:', user.id);Sign in with zero-knowledge verification
// Authenticate user
const session = await zkauth.signIn(
'user@example.com',
'secure-password'
);
if (session.success) {
console.log('Authenticated:', session.user);
}Here's a complete working example you can copy and run
Full example with error handling and user feedback
import { ZKAuth } from '@zkauth/sdk';
// Initialize ZKAuth
const zkauth = new ZKAuth({
apiKey: 'zka_...',
baseUrl: 'https://api.zkauth.com'
});
// Complete authentication flow
async function authenticateUser() {
try {
// Register new user
const user = await zkauth.signUp('user@example.com', 'password');
console.log('User registered:', user.id);
// Sign in user
const session = await zkauth.signIn('user@example.com', 'password');
if (session.success) {
console.log('Authentication successful!');
console.log('User:', session.user);
console.log('Session token:', session.token);
} else {
console.error('Authentication failed:', session.error);
}
} catch (error) {
console.error('Error:', error);
}
}
// Run the authentication
authenticateUser();See how to integrate ZKAuth with popular frameworks
React hooks and components
import { ZKAuthProvider, useZKAuth } from '@zkauth/react';
function App() {
return (
<ZKAuthProvider apiKey="zka_...">
<LoginForm />
</ZKAuthProvider>
);
}
function LoginForm() {
const { signIn, user, loading } = useZKAuth();
const handleSubmit = async (e) => {
e.preventDefault();
const result = await signIn('user@example.com', 'password');
if (result.success) {
console.log('Logged in!');
}
};
return (
<form onSubmit={handleSubmit}>
{/* Your form fields */}
</form>
);
}Server-side authentication
// pages/api/auth/[...nextauth].js
import NextAuth from 'next-auth';
import { ZKAuthProvider } from '@zkauth/nextjs';
export default NextAuth({
providers: [
ZKAuthProvider({
clientId: process.env.ZKAUTH_CLIENT_ID,
clientSecret: process.env.ZKAUTH_CLIENT_SECRET,
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.zkProof = user.zkProof;
}
return token;
},
},
});
// pages/login.js
import { signIn, useSession } from 'next-auth/react';
export default function Login() {
const { data: session } = useSession();
const handleLogin = () => {
signIn('zkauth');
};
return (
<button onClick={handleLogin}>
Sign in with ZKAuth
</button>
);
}Node.js middleware
const express = require('express');
const { ZKAuthMiddleware } = require('@zkauth/nodejs');
const app = express();
const zkAuth = new ZKAuthMiddleware({
apiKey: process.env.ZKAUTH_API_KEY
});
// Protected route middleware
const requireAuth = zkAuth.authenticate();
// Public route
app.get('/', (req, res) => {
res.send('Welcome to ZKAuth Demo');
});
// Protected route
app.get('/dashboard', requireAuth, (req, res) => {
res.json({
message: 'Protected data',
user: req.user
});
});
// Login route
app.post('/login', async (req, res) => {
try {
const { email, password } = req.body;
const session = await zkAuth.signIn(email, password);
if (session.success) {
res.json({ success: true, user: session.user });
} else {
res.status(401).json({ error: 'Invalid credentials' });
}
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});Continue building with ZKAuth