Quick Start Guide

Quick Start

Get ZKAuth running in your application in under 5 minutes with this step-by-step guide.

5 minutes
4 steps

Get Started in 4 Steps

Follow these simple steps to integrate ZKAuth into your application

1

Install ZKAuth

30 seconds

Add ZKAuth to your project

bash
npm install @zkauth/sdk
2

Initialize Client

1 minute

Set up the ZKAuth client

javascript
import { ZKAuth } from '@zkauth/sdk';

const zkauth = new ZKAuth({
  apiKey: 'zka_...',
  baseUrl: 'https://api.zkauth.com'
});
3

Create User

2 minutes

Register a new user with ZK proof

javascript
// Register a new user
const user = await zkauth.signUp(
  'user@example.com',
  'secure-password'
);

console.log('User created:', user.id);
4

Authenticate

1 minute

Sign in with zero-knowledge verification

javascript
// Authenticate user
const session = await zkauth.signIn(
  'user@example.com',
  'secure-password'
);

if (session.success) {
  console.log('Authenticated:', session.user);
}

Complete Example

Here's a complete working example you can copy and run

Complete Authentication Flow

Full example with error handling and user feedback

javascript
javascript
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();

Framework Examples

See how to integrate ZKAuth with popular frameworks

React

React hooks and components

javascript
javascript
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>
  );
}

Next.js

Server-side authentication

javascript
javascript
// 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>
  );
}

Express.js

Node.js middleware

javascript
javascript
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');
});

What's Next?

Continue building with ZKAuth

Ready to Build?

Start building secure applications with ZKAuth today.