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.

Validating user creation

Validating user creation

Currently, we simply enabled the add user functionality, but it is very crucial to have proper validations so that the code does not end up in error situations. So let's begin with checking if the user exists or not. If the user exists, then we will not add the user. So first, we'll search for the same user by saying const existingUser is equal to await db.adminUser.findUnique. Here I am going to give the WHERE clause and will give username colon data.username. Now if the user doesn't exist, then we will redirect to the addUser page with an error message and to send the error message we will use the search params. So here I will write if existing user then return will say redirect and here I'll say slash user slash add question mark error message is equal to username already exists. So now if there is an existing user, then the code will return the redirect method, which means that it will exit the method and the user creation code will not be executed. Now let's go to the page JSX of…

Contents