Create

bin/rails generate authentication

It creates User and Session models and the controllers and views necessary to login to our application.

Log Out

<!-- app/views/layouts/application.html.erb -->
<!DOCTYPE html>
<html>
  <!-- ... -->
  <body>
    <nav>
      <%= link_to "Home", root_path %>
      <%= button_to "Log out", session_path, method: :delete if authenticated? %>
    </nav>
 
    <main>
      <%= yield %>
    </main>
  </body>
</html>

Unauthenticated Access

class ProductsController < ApplicationController
  allow_unauthenticated_access only: %i[ index show ]
  # ...
end

Showing Links for (Un)Authenticated Users Only

<%= link_to "New product", new_product_path if authenticated? %>

NOTE: if authenticated?

<%= link_to "Login", new_session_path unless authenticated? %>

NOTE: new_session_path