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.

Implementing logout with server action

Implementing logout with server action

In this lecture, we will implement the logout user functionality using server action. So by clicking on this button, the user will be logged out. And to log out the user, we will just need to delete the token from the cookie and redirect the user to the login page. So let's create a function to delete the cookie from the server. As we discussed earlier, the cookie can be deleted from the server side itself. So I will create a function inside the auth actions.js, which will delete the cookie. I'll name it delete cookie, and it will receive the cookie name as parameter. Now inside I will use the cookies.delete method to delete the cookie. This method will take a cookie name as an argument. So I'll pass the name here. Now this delete method is actually an asynchronous method. So I will make this function async. All right, now I will create another function to log out the user by saying export async function logout user and will call the delete cookie method and pass the name JWT token…

Contents