naystack - v1.9.9
    Preparing search index...

    Function setupEmailAuth

    • 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_KEY from env vars. Optionally supports Cloudflare Turnstile captcha validation when TURNSTILE_KEY is set.

      Parameters

      • options: SetupEmailAuthOptions

        Configuration for the auth routes. See SetupEmailAuthOptions.

        Options for initializing email auth routes (GET/POST/PUT/DELETE) via setupEmailAuth.

        • OptionalallowedOrigins?: string[]

          Optional list of allowed CORS origins. If set, cross-origin requests from unlisted origins are rejected with 403.

        • createUser: (user: any) => Promise<UserOutput | undefined>

          Creates a new user with the hashed password; returns the created user as UserOutput.

        • getUser: (data: any) => Promise<UserOutput | undefined>

          Fetches user by request data (e.g. { email }); used for login and sign-up duplicate check. Must return a UserOutput or undefined.

        • OptionalonError?: ErrorHandler

          Optional 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).

      Returns {
          DELETE: (req: NextRequest) => Promise<NextResponse<unknown>>;
          GET: (req: NextRequest) => Promise<NextResponse<unknown>>;
          OPTIONS?: (req: NextRequest) => NextResponse<unknown>;
          POST: (req: NextRequest) => Promise<NextResponse<unknown> | undefined>;
          PUT: (req: NextRequest) => Promise<NextResponse<unknown> | undefined>;
      }

      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 { setupEmailAuth } from "naystack/auth";

      export const { GET, POST, PUT, DELETE } = setupEmailAuth({
      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)
      },
      });