Authentication
Import server helpers from "void/auth". Import client helpers from "void/client".
On this pageConfigure auth with defineAuth · Read the current user or session · Protect a route with requireAuth
defineAuth(config)
An advanced escape hatch. Use it to customize the SDK's Better Auth config.
import { defineAuth } from 'void/auth';
export default defineAuth(({ defaults }) => ({
...defaults,
trustedOrigins: ['https://example.com'],
}));Signature:
function defineAuth(config: VoidAuthConfig): VoidAuthConfig;getUser()
Returns the signed-in user, or null if there is none.
import { getUser } from 'void/auth';
const user = getUser();Signature:
function getUser(): AuthUser | null;getSession()
Returns the current Better Auth request state, or null.
import { getSession } from 'void/auth';
const state = getSession();Signature:
function getSession(): AuthState | null;requireAuth(c)
Gets the signed-in user from the request context. Throws a 401 HTTPException if there is no session.
import { defineHandler } from 'void';
import { requireAuth } from 'void/auth';
export const GET = defineHandler((c) => {
const user = requireAuth(c);
return { email: user.email };
});Signature:
function requireAuth(c: CloudContext): AuthUser;AuthUser
The Better Auth user shape, re-exported by the SDK.
interface AuthUser {
id: string;
email: string;
emailVerified: boolean;
name: string;
image?: string | null;
createdAt: Date;
updatedAt: Date;
}AuthSession
The Better Auth session shape, re-exported by the SDK.
interface AuthSession {
id: string;
token: string;
userId: string;
expiresAt: Date;
createdAt: Date;
updatedAt: Date;
ipAddress?: string | null;
userAgent?: string | null;
}AuthState
The combined state of a signed-in request:
interface AuthState {
user: AuthUser;
session: AuthSession;
}auth
Imported from "void/client". A ready-to-use Better Auth client, set up with basePath: "/api/auth".
import { auth } from 'void/client';
await auth.signIn.email({ email, password });
await auth.signOut();The client methods come from Better Auth. In pages apps, the SDK picks the Better Auth client package for your framework when one exists.
Some SPA apps use the SDK only for API and file routes, without Pages mode. They must import the framework client directly:
import { auth } from 'void/client/react';
// or: 'void/client/vue'
// or: 'void/client/svelte'
// or: 'void/client/solid'These subpaths keep the same fetch, fetchStream and auth exports as void/client. They bind auth and createAuthClient to Better Auth's framework package.
createAuthClient
Imported from "void/client". A re-export of Better Auth's createAuthClient for advanced use.
import { createAuthClient } from 'void/client';
const auth = createAuthClient({ basePath: '/api/auth' });