naystack - v1.5.10
    Preparing search index...

    Function getEmailAuthRoutes

    • 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: InitRoutesOptions

        Configuration for the auth routes. See InitRoutesOptions.

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

        • 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<
                  { accessToken: string
                  | undefined; refreshToken: string | undefined },
              >,
          >;
          GET: (
              req: NextRequest,
          ) => Promise<
              NextResponse<
                  { accessToken: string
                  | undefined; refreshToken: string | undefined },
              >,
          >;
          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 { 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)
      },
      });