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.

API to fetch unique customer data

API to fetch unique customer data

In the previous lecture, we implemented the login functionality. Now once the user is logged in, we want to display the user name like this. And for that we need to fetch the unique users data. So we will create an API to fetch the customers data using the JWT token. And this token will be provided in the cookie. So I will navigate to the admin directory and create a folder called customer inside the API directory and I will create a route.js file inside it. Now in the route.js, I will write export async function get and will receive the request here. Now let's implement the try catch block inside that function where we will return the next response dot json with the message something went wrong error error dot message and the status 500. Now I want to retrieve the customer's data and to do that I will get the JWT object using verify JWT function. The JWT object will contain the user ID and using it we will fetch the customers data. So, I will take the JWT token from the request by…

Contents