We've integrated Axios to handle API calls. By default, it uses the current host URL, but you can customize this in the .env file to suit your requirements.
.env
...
NEXT_PUBLIC_API_HOST=
...
Axios has been configured in the folder src/utils/axios.ts
Auth api work scenario
We have our all API's in the src/app/api directory. In this directory, there is a folder named auth, which contains our authentication-related APIs.
The AUTH_PROVIDER can be set to either mockor supabase. Both options utilize the same API endpoint, with the backend dynamically invoking the corresponding business logic based on the AUTH_PROVIDERconfiguration.
The authProvider.ts file manages this distinction by dynamically loading and returning the appropriate authentication provider module.
Below code defines a POST API route handler that dynamically invokes the appropriate login function based on the currently configured authentication provider.
src\app\api\auth\login\route.ts
// @next
import { NextResponse } from 'next/server';
// @project
import { authProvider } from '@/app/api/auth/authProvider';
export async function POST(request: Request) {
try {
const authProviderHandler = await authProvider();
// Check if `login` is defined and is a function
if (authProviderHandler.login) {
return await authProviderHandler.login(request);
} else {
return NextResponse.json({ error: 'Login functionality not available' }, { status: 404 });
}
} catch {
return NextResponse.json({ error: 'Server error' }, { status: 500 });
}
}
For authentication, we have two distinct business logic implementations based on the selected AUTH_PROVIDER:
Mock: The mock authentication logic is defined in src/app/api/mock/auth/index.ts.