All skills
clerk avatar

/clerk-react-patterns

@ca5e365 official
by clerkclerk/skills83 stars
5

React SPA auth patterns with @clerk/react for Vite/CRA - ClerkProvider setup, useAuth/useUser/useClerk hooks, React Router protected routes, custom sign-in flows. Triggers on: Vite Clerk setup, React Router auth, useAuth hook, protected route, custom sign-in form React.

Use this Skill: https://skilld.dev/gh/clerk/skills/clerk-react-patterns

This session only. Nothing lands on disk.

referencescustom-flows.md

≈686 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Custom Flows (HIGH)

Custom Sign-In with useSignIn

import { useSignIn } from '@clerk/react'
import { useNavigate } from 'react-router-dom'
import { useState } from 'react'

export function CustomSignIn() {
  const { isLoaded, signIn, setActive } = useSignIn()
  const navigate = useNavigate()
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')
  const [error, setError] = useState('')

  async function handleSubmit(e: React.FormEvent) {
    e.preventDefault()
    if (!isLoaded) return

    try {
      const result = await signIn.create({
        identifier: email,
        password,
      })

      if (result.status === 'complete') {
        await setActive({ session: result.createdSessionId })
        navigate('/dashboard')
      }
    } catch (err: unknown) {
      const clerkError = err as { errors?: Array<{ message: string }> }
      setError(clerkError.errors?.[0]?.message ?? 'Sign in failed')
    }
  }

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="email"
        value={email}
        onChange={e => setEmail(e.target.value)}
        placeholder="Email"
      />
      <input
        type="password"
        value={password}
        onChange={e => setPassword(e.target.value)}
        placeholder="Password"
      />
      {error && <p>{error}</p>}
      <button type="submit">Sign In</button>
    </form>
  )
}

Custom Sign-Up with useSignUp

import { useSignUp } from '@clerk/react'

export function CustomSignUp() {
  const { isLoaded, signUp, setActive } = useSignUp()

  async function handleSubmit(email: string, password: string) {
    if (!isLoaded) return

    const result = await signUp.create({ emailAddress: email, password })

    if (result.status === 'complete') {
      await setActive({ session: result.createdSessionId })
    } else if (result.status === 'missing_requirements') {
      // Email verification needed
      await signUp.prepareEmailAddressVerification({ strategy: 'email_code' })
    }
  }
}

Email Verification

async function verifyEmail(code: string) {
  const result = await signUp.attemptEmailAddressVerification({ code })

  if (result.status === 'complete') {
    await setActive({ session: result.createdSessionId })
    navigate('/dashboard')
  }
}

signIn.status Values

Status Meaning
'complete' Auth successful, call setActive
'needs_first_factor' First factor required (e.g., password)
'needs_second_factor' MFA required
'needs_new_password' Password reset required

Always check result.status === 'complete' before calling setActive.

Docs

Source: SKILL.md on GitHub

No alerts17d4 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill provides standard documentation, reference patterns, and templates for implementing Clerk authentication in React single-page applications. It follows best practices for authentication flows and uses official dependencies.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at ca5e365. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 6 months ago
What it can do
Network
metadata
{
  "author": "clerk",
  "version": "1.0.0"
}
All 1 allowed tools
WebFetch
  • React
  • clerk
  • authentication
  • vite
  • react-router
  • hooks
  • spa
  • protected-routes

README badge

README badge for clerk/skills/clerk-react-patterns

Provides patterns for @clerk/react authentication in Vite and Create React App SPAs, including ClerkProvider setup, useAuth/useUser/useClerk hooks, React Router protected routes, and custom sign-in flows. Covers client-side auth state management and token retrieval for API calls with React Router v6/v7.

Generated from the current SKILL.md.

Does this skill work with Next.js?
No. This skill is for Vite and Create React App SPAs using @clerk/react. For Next.js, use the clerk-nextjs-patterns skill instead.
Do I need to check isLoaded before using isSignedIn?
Yes. isLoaded must be true before trusting isSignedIn. Always guard on isLoaded first to avoid undefined values.
How do I get a JWT token to send with API requests?
Call getToken() from the useAuth hook, which returns a session JWT. Always null-check the result before using it.
How do I protect routes with React Router?
Create a ProtectedRoute component that checks isLoaded and isSignedIn with useAuth, then redirects unsigned users or renders an Outlet for protected children.

Generated from the current SKILL.md. These answers refresh after source changes.