SaaSDay
TUTORIALS
API Routes

API Routes

Folder app/api is used to define the API endpoints. These are server side components that are used to interact with the database and other services.

SaaSDay uses Nextjs 14 app router and route handlers are defined in route.ts file inside the folder.

Protected API Routes

To protect an API route, you can use the auth utility. This will check if the user is authenticated and if not, it will redirect the user to the login page.

    const session = await auth();
    if (!session?.user || session?.user.id !== userId) {
            redirect('/login')
    }