clerk-react-patterns

Provide Clerk authentication patterns for React single-page applications.

1|Updated Apr 11, 2026
One-click install
npx skills add https://github.com/RajAryanIITBHU/bny --skill clerk-react-patterns-rajaryaniitbhu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-react-patterns
Source: https://github.com/RajAryanIITBHU/bny/tree/main/.continue/skills/clerk-react-patterns
Command: npx skills add https://github.com/RajAryanIITBHU/bny --skill clerk-react-patterns-rajaryaniitbhu

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides practical, mistake-resistant patterns for adding authentication to React single-page applications with @clerk/react, reducing integration errors and insecure implementations.

Core Features & Use Cases

  • Provider setup: guidance for placing ClerkProvider at the app root and loading the publishable key from environment variables.
  • Auth hooks: concrete usage patterns for useAuth, useUser, and useClerk including the isLoaded guard and getToken for API calls.
  • Protected routes & router integration: ProtectedRoute patterns for React Router v6/v7, org-gated routes, and component-level Show fallbacks.
  • Custom flows & templates: examples for building custom sign-in and sign-up forms and ready-to-run Vite template scaffolds for fast bootstrapping. Use case example: bootstrap a Vite app, wrap the app with ClerkProvider, protect dashboard routes with a ProtectedRoute, and call a backend API with a session token retrieved via getToken.

Quick Start

Add ClerkProvider to your main file, read VITE_CLERK_PUBLISHABLE_KEY from import.meta.env, and implement a ProtectedRoute that returns a loading state while isLoaded is false and redirects to /sign-in when isSignedIn is false.

Frequently Asked Questions about clerk-react-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I protect routes in a React single-page application using Clerk?

To protect routes in React with Clerk, implement a ProtectedRoute component using React Router v6/v7 that returns a loading state while the useAuth isLoaded hook is false and redirects to /sign-in when isSignedIn is false. This prevents unauthorized access to secure dashboard areas.

What is the correct way to set up ClerkProvider in a Vite React project?

Setting up ClerkProvider in a Vite React project requires placing the provider at the app root in your main file and loading the publishable key from the VITE_CLERK_PUBLISHABLE_KEY environment variable via import.meta.env. This ensures authentication context is available across the entire application.

How do I get session tokens for API calls from Clerk React hooks?

You get session tokens for API calls in Clerk React hooks by using the getToken function from the useAuth hook. This provides a session token you can attach to backend API requests to authenticate and authorize user actions securely.

Can I build custom sign-in and sign-up forms using Clerk with React?

Yes, you can build custom sign-in and sign-up forms using Clerk with React through the useClerk hook. This allows you to create tailored authentication flows while leveraging Clerk's underlying security and session management infrastructure.

Why does my React app crash or show blank screens before Clerk authentication loads?

Your React app crashes or shows blank screens before Clerk loads because authentication state is uninitialized. You must implement an isLoaded guard from the useAuth or useUser hooks to render a loading fallback until the Clerk session state fully resolves.

Does Clerk React authentication work with both Vite and Create React App?

Yes, Clerk React authentication works with both Vite and Create React App projects. The integration patterns apply universally, though environment variable access differs slightly between Vite's import.meta.env and Create React App's process.env syntax.