When using devise, one usually uses a before_action
to authenticate a user:
before_action :authenticate_user!
This always requires a login though. What if you want to login a user if possible, but ignore it if the user is not logged in? You need to use warden (the underlying authentication framework devise uses) directly:
class MyController < ApplicationController
skip_before_action :authenticate_user!
def show
user = warden.authenticate(scope: :user)
if user
# Logged in
else
# Not logged in
end
end
end