Configuration for the auth routes. See InitRoutesOptions.
Options for initializing email auth routes (GET/POST/PUT/DELETE) via getEmailAuthRoutes.
Creates a new user with the hashed password; returns the created user as UserOutput.
Fetches user by request data (e.g. { email }); used for login and sign-up duplicate check. Must return a UserOutput or undefined.
OptionalonError?: ErrorHandlerOptional custom handler for validation/auth errors; return a NextResponse to override default error responses.
OptionalonLogin?: (userId: number | null, body: any) => Promise<void>Optional callback after successful login. Receives (userId, requestBody).
OptionalonLogout?: (userId: number | null, body: any) => Promise<void>Optional callback when DELETE logout is used. Receives (userId, requestBody).
OptionalonRefresh?: (userId: number | null, body: any) => Promise<void>Optional callback when GET refresh is used. Receives (userId, requestBody).
OptionalonSignUp?: (userId: number | null, body: any) => Promise<void>Optional callback after successful sign-up. Receives (userId, requestBody).
Object with GET (refresh tokens), POST (sign up), PUT (login), DELETE (logout) — export as your route's handlers.
// app/api/(auth)/email/route.ts
import { getEmailAuthRoutes } from "naystack/auth";
export const { GET, POST, PUT, DELETE } = getEmailAuthRoutes({
getUser: async ({ email }: { email: string }) => {
const [user] = await db.select({ id: UserTable.id, password: UserTable.password })
.from(UserTable).where(eq(UserTable.email, email));
return user;
},
createUser: async (data) => {
const [user] = await db.insert(UserTable).values(data)
.returning({ id: UserTable.id, password: UserTable.password });
return user;
},
onSignUp: async (userId, body) => {
// Optional: run logic after sign-up (e.g. create an org)
},
});
Returns Next.js route handlers for email auth. Mount them in your auth API route (e.g.
app/api/(auth)/email/route.ts).The library automatically hashes passwords on sign-up and reads
SIGNING_KEY/REFRESH_KEYfrom env vars. Optionally supports Cloudflare Turnstile captcha validation whenTURNSTILE_KEYis set.