DEV Community

austinmei5
austinmei5

Posted on

Sign in/Sign out

In the nav bar, paste the following to create sign in/sign out functionality:

<ul class="navbar-nav">
  <% if user_signed_in? %>
    <li class="nav-item">
      <%= link_to "Edit profile", edit_user_registration_path, class: "nav-link" %>
    </li>

    <li class="nav-item">
      <%= link_to "Sign out", destroy_user_session_path, class: "nav-link", method: :delete %>
    </li>
  <% else %>
    <li class="nav-item">
      <%= link_to "Sign in", new_user_session_path, class: "nav-link" %>
    </li>

    <li class="nav-item">
      <%= link_to "Sign up", new_user_registration_path, class: "nav-link" %>
    </li>
  <% end %>
</ul>
Enter fullscreen mode Exit fullscreen mode

To enforce sign in/out, we add a before action in ApplicationController:

  1. Add: "before_action :authenticate_user!"

Top comments (0)