Tutorials
User Authentication Guide
Learn how to implement authentication in your Indie Kit app using useUser hook, auth() function, and withAuthRequired middleware
User Authentication Guide 🔐
Indie Kit provides powerful authentication tools that you can use throughout your application. Let's explore how to implement authentication in different contexts! 🚀
Client-Side Authentication with useUser 💻
The useUser
hook is perfect for client components. It provides user data and loading states:
Features of useUser 🎯
- 🔄 Automatic revalidation
- 🎭 Type-safe user data
- ⌛ Loading states
- 🚫 Error handling
- 🔌 Offline support
Server-Side Authentication with auth() 🔒
For server components and API routes, use the auth()
function:
When to Use auth() 📋
- ✅ Server Components
- ✅ API Routes
- ✅ Server Actions
- ✅ Middleware
- ✅ Layout Components
Protected API Routes with withAuthRequired 🛡️
Secure your API routes using the withAuthRequired
middleware:
Benefits of withAuthRequired 🎯
- 🔒 Automatic authentication checking
- 🎭 Type-safe session data
- 🚫 Automatic error responses
- 📝 Session context in handler
- ⚡ Zero-config setup
Authentication Flow Examples 🌊
Protected Dashboard Page
User Settings Component
Best Practices 💫
-
Choose the Right Method
- 🖥️
useUser
for client components - 🔒
auth()
for server components - 🛡️
withAuthRequired
for API routes
- 🖥️
-
Error Handling
- Always handle loading states
- Provide clear error messages
- Implement proper redirects
-
Security Tips
- Validate on both client and server
- Never expose sensitive data
- Use HTTPS in production
- Implement proper CORS policies
-
Performance
- Cache authentication state
- Use loading skeletons
- Implement proper revalidation
Now you're ready to implement authentication throughout your Indie Kit application! Remember to always validate user permissions and handle edge cases appropriately. 🚀