From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Fetching customer data using API in the client section

Fetching customer data using API in the client section

In the previous lecture, we have created the API to fetch the logged in customers data. Now I will integrate this API into the client section. We will use server actions to fetch the data from the API. So let me navigate to the client directory and open the auth actions JS file. Now I will create a server action by saying export async function getCustomerData. Let me call the API to fetch the customer data. Const response is equal to await fetch base URL slash API slash customer then credentials include with headers. I'll pass cookie customer JWT token is equal to will join with get cookie method and passing the customer JWT token. If the API gives an error, then I want to delete the token from the cookie instead of redirecting the user to the login page, because the customer can access most of the routes without being logged in. So I will create a function to delete the cookie inside the auth actions JS file by saying export async function delete cookie will receive name here then…

Contents