Usage & Integration
Learn how to implement Clerk’s authentication features, such as sign-in, protected routes, and user management, in your app.
Clerk provides components and hooks for authentication flows, integrated into Plainform Dev’s Next.js structure, allowing you to manage user sessions and protect routes easily.
Implementing Authentication
Add Authentication Components
- Use Clerk’s
<SignIn />or<SignUp />in app or components. Example:
import { SignIn } from '@clerk/nextjs';
export default function SignInPage() {
return <SignIn />;
}Protected Routes
- Restrict access using Clerk’s
author middleware. Example:
import { auth } from "@clerk/nextjs";
export default function ProtectedPage() {
const { userId } = auth();
if (!userId) return <div>Please sign in</div>;
return <div>Protected content</div>;
}Manage user Data
- Use Clerk hooks like
useUserin components:
import { useUser } from '@clerk/nextjs';
export default function UserMenu() {
const { user } = useUser();
return <div>Welcome, {user?.firstName}</div>;
}Verify Configuration
- Run
npm run devto validate keys viat3-env(inenv.mjs). - Check the Clerk dashboard if validation fails.
Handle Webhooks (Optional)
- Configure
CLERK_WEBHOOK_SECRETin.envfor webhook verification. - Create an API route (e.g.,
app/api/webhooks/clerk/route.ts) for webhook events.
Testing Authentication
- Run
npm run devand test/sign-inor/sign-uppages. - Verify protected routes restrict unauthenticated users.
- Check user data rendering with Clerk hooks.
- Monitor user activity in the Clerk dashboard.
Important Notes
- Environment Variables: Ensure
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEYandCLERK_SECRET_KEYare valid, ort3-envwill block startup. - Customization: Adjust Clerk’s UI (e.g., branding, themes) in the dashboard to match your app.
- Security: Keep
CLERK_SECRET_KEYandCLERK_WEBHOOK_SECRETout of client-side code and version control.
By following these steps, you can leverage Clerk’s robust authentication features in Plainform Dev, streamlining user management for your SaaS. For advanced features like social logins or 2FA, consult the Clerk documentation.
How is this guide ?
Last updated on