Authentication

Learn how authentication works in the NextCoder platform

Overview

NextCoder uses Clerk for authentication, providing secure user management with support for:

  • Email/password authentication
  • Magic link authentication
  • OAuth with Google and GitHub
  • Session management
  • User profile management

Session Handling

Authentication is handled automatically through Clerk's session management. The session is available throughout the application via Clerk's React hooks.

import { useUser } from '@clerk/nextjs';

function MyComponent() {
  const { isLoaded, isSignedIn, user } = useUser();
  
  if (!isLoaded) {
    return <div>Loading...</div>;
  }
  
  if (!isSignedIn) {
    return <div>Not signed in</div>;
  }
  
  return <div>Hello, {user.firstName}!</div>;
}

API Authentication

All API endpoints require authentication through Clerk session cookies. The authentication is handled automatically when making requests from the browser.

Protecting Routes

Routes are protected using Clerk's middleware. Public routes are explicitly defined in the middleware configuration.

// middleware.ts
import { authMiddleware } from "@clerk/nextjs/server";

export default authMiddleware({
  publicRoutes: ["/", "/api/webhooks(.*)", "/pricing"],
});

export const config = {
  matcher: ["/((?!.+\.[\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};