Next.js SDK

Next.js integration providing a Client Component and Server Action helper

Installation

terminal
npm install @gkcaptcha/next

Environment Variables

.env.local
# .env.local
GKCAPTCHA_SITE_KEY=pk_live_...
GKCAPTCHA_SECRET_KEY=sk_live_...
# Optional: override API URL (defaults to https://gkcaptcha.gatekeeper.sa)
# GKCAPTCHA_API_URL=https://gkcaptcha.gatekeeper.sa

Client Component

Import from @gkcaptcha/next in Client Components. / استورد من @gkcaptcha/next في مكوّنات العميل.

app/login/page.tsx
// app/login/page.tsx
'use client'
import { GkCaptcha, useGkCaptcha } from '@gkcaptcha/next'
import { useState } from 'react'

export default function LoginPage() {
  const { ref, reset } = useGkCaptcha()
  const [token, setToken] = useState('')

  return (
    <form action={loginAction}>
      <GkCaptcha
        ref={ref}
        siteKey={process.env.NEXT_PUBLIC_GKCAPTCHA_SITE_KEY!}
        onVerify={setToken}
        onExpire={() => setToken('')}
      />
      <input type="hidden" name="captchaToken" value={token} />
      <button type="submit" disabled={!token}>Login</button>
    </form>
  )
}

Server Action

Import from @gkcaptcha/next/server in Server Actions and API Routes. / استورد من @gkcaptcha/next/server في Server Actions ومسارات API.

app/actions/login.ts
// app/actions/login.ts
'use server'
import { verifyCaptchaToken } from '@gkcaptcha/next/server'

export async function loginAction(formData: FormData) {
  const token = formData.get('captchaToken') as string
  const result = await verifyCaptchaToken(token)

  if (!result.success) {
    return { error: 'CAPTCHA verification failed' }
  }

  // Proceed with login...
}

API Route

app/api/auth/route.ts
// app/api/auth/route.ts
import { verifyCaptchaToken } from '@gkcaptcha/next/server'

export async function POST(request: Request) {
  const body = await request.json()
  const result = await verifyCaptchaToken(body.captchaToken)

  if (!result.success) {
    return Response.json({ error: 'CAPTCHA failed' }, { status: 403 })
  }

  // Proceed...
}

Import Paths

Import / الاستيرادUse In / الاستخدام فيContains / يحتوي على
@gkcaptcha/nextClient ComponentsGkCaptcha, useGkCaptcha
@gkcaptcha/next/serverServer Actions, API RoutesverifyCaptchaToken

Never import from @gkcaptcha/next/server in a Client Component — it reads server-only environment variables. / لا تستورد أبدًا من @gkcaptcha/next/server في مكوّنات العميل — فهو يقرأ متغيرات البيئة الخاصة بالخادم.

Props

See React SDK for the full prop table — GkCaptcha has the same API. / انظر توثيق React SDK للحصول على جدول الخصائص الكامل.