From the course: React: Authentication

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Adding log-out functionality

Adding log-out functionality - React.js Tutorial

From the course: React: Authentication

Adding log-out functionality

- [Instructor] All right, so the last thing that we have to do here, as I said, is implement some logout functionality. So let's go into our UserInfoPage and we can also close most of these here too. So we'll just close all of those like so, and open our UserInfoPage back up. And now all we have to do is go back down to the logOut function that's already in here and basically just rewrite it so that it logs the user out. Now this is pretty easy to do. All we really have to do is remove, and oops, that's the wrong one there. All we really need to do is remove the token from localStorage and send the user back to the login route, right? We're just going to automatically redirect the user there. So here's what this is going to look like. All we really have to do is call setToken here. And we're going to set that to undefined, right? That's going to remove it from the state anyway. And we might also want to open up a TokenContext here and basically add a case for that. So we'll say, if…

Contents