Enable Users to Logout and Dynamically Render View Content in Rails 5
This guide examines how to implement the ability for users to be able to logout of a Rails 5 application. Additionally this lesson shows how to dynamically show and hide the authentication links based on whether a user is signed in or out.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Our applications coming along quite nicely but we have a little bit of an issue with our authentication system, we don't have the ability to log out.

If I come to localhost:3000/login it is going to say "You are already signed in." We need the ability to sign out. It wouldn't be a great user interface if you did not have that.

Let's come in and build that, now we don't have to change our routes but we do need to add a logout button. We're going to put it here (application.html.erb) eventually we're going to put it in the nav bar. For right now, because we haven't got to our design section yet, we're going to put a link to it.

One thing that I think is important to check out, how do we know what our path is going to be? I could just type it in because I have created so many logout buttons that I could do this in my sleep. If you have not done it a million times you find it by typing in rake routes | grep logout

large

We see that we have one route and it is destroy user session with a method of delete. If you're curious on how you would find a path, just come type rake routes and that is going to show you what it is. So it's going to be destroy_user_session path and we also need to pass in the method delete.

I'm going to say <%= link_to "Logout", destroy_user_session_path, method: :delete %>

Let's see if this is working, this isn't totally done yet but let's see if at least works up to this point. We see our logout button there, if I click it, it says "Signed out successfully."

If I go to localhost:3000/register you can see all of this is working. Like I mentioned, it's not quite done because we have this logout button and it's just going to keep on saying "Signed out successfully." That's not what we want, we only want the logout button shown if a user is already logged in.

The way that we can do that is inside of our view we place a statement that says "if current user, then we want to show the logout button and if not, "else" which means that there is no user signed in. Else, and inside of this I'm going to paste in some other links so that they can sign in. Here we'll create two of them, say register or login. Let's delete these method delete items. Let's go look up the routes for register and login.

Run `rake routes | grep user

large

This will tell us exactly where to find our user route names, we want new session. If you are curious It says it right here, if we need to get a new user session, meaning that we want to have a user log in this is the path for us.

I'm going to copy this, instead of saying destroy user session for this login, it's going to say new user session.

For register, if you come down you see we have register as a path right here. For this one I can say new user registration

large

Hit save, now this should work and be dynamic. If I hit refresh, our logout button is gone. If we hit login, that works and now it says logout.

This is the type of behavior we are looking for. If I hit register, I can sign up with some other credentials and everything is still working perfectly.

Great job if you went through that, you now have a fully functional login and logout process. Let's stop the server.

  • git status
  • git add .
  • git commit -m "Implemented logout functionality and dynamic view rendering for auth."
  • git push origin authentication

We can come to pivotal tracker and we have finished our custom routes.

In the next guide we're going to talk about customizing our attributes. I remember one that I wanted to add, which is to integrate virtual attributes. Now we are good to go, I'll see you in the next guide.

Resources