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.

Creating login API

Creating login API

To log in the customer, we will create an API in the admin section, which validates the customer's data with the buyer master table. For that, we will create API on the route, API slash auth slash login, which will handle the post request. So inside the auth directory, I will create a login folder, and I will add a route.js file inside it. Now in the route.js file, I will say export async function post and will receive the request object here. Then I will use the try catch block and if there is any error, then I will return the JSON response with message, something went wrong and status 500. Alright, now inside the try block, I will take the request data in JSON format. I'll say const data is equal to await request dot json. And to check the existence of the user, let me fetch the user data. So I will take a constant existing customer and try to fetch the data using find unique method. And in the where clause, I'll give e-mail is equal to data dot e-mail. If the user doesn't exist…

Contents