Piva uses Clerk for authentication combined with custom middleware and database integration for user management.
- Handles user sign-in/sign-up
- Manages authentication state
- Provides user tokens and session management
2. Middleware (middleware.ts)
const publicRoutes = [
'/',
'/sign-in(.*)',
'/sign-up(.*)',
'/api(.*)',
'/live-webinar(.*)',
'/callback(.*)',
];- Protects all non-public routes
- Redirects unauthenticated users to sign-in
- Allows public access to specified routes
- User visits protected route
- Middleware checks authentication
- If authenticated:
- Access granted
- User data synced with database
- If not authenticated:
- Redirect to sign-in
- After sign-in, return to original route
4. Server-Side Authentication (auth.ts)
export const onAuthenticateUser = async () => {
const { userId } = auth();
if (!userId) return { user: null };
const user = await prismaClient.user.findUnique({
where: { id: userId },
});
return { user };
};- Validates user sessions
- Syncs with database
- Used in server actions and API routes
- Wrapped in
(protectedRoutes)layout - Require valid authentication
- Automatic redirect if session expires
- Full access to app features
- Landing page
- Authentication pages
- Public webinar views
- API endpoints
- No authentication required