简体   繁体   中英

Devise - How to trigger a sign_in on page user already has access to

I am using Devise 3.5.2. I have an Item model with a show action that anyone can access( before_filter :authenticate_user!, except: :show ). On the show page I have a button that triggers a file upload dialog using jquery.

I only want this upload dialog to work for logged in users. If a user is logged out and they click the button I want it to bring them to my login page. When they login I want it to take them back to the show page.

How can I make it so clicking this link brings them to the login page and then after logging in they are redirected to the page they were just on and already had access to?

<% unless current_user %>
  <%= link_to "Upload image", item_path @item, class: "btn btn-lg btn-success" %>
<% end %>

This won't work because they already have access to this page.

EDIT: I tried following this link here https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in but it doesn't seem to be working.

My sessions controller looks like this. Maybe my create method is causing a conflict?

class SessionsController < Devise::SessionsController

   def new
     self.resource = resource_class.new(sign_in_params)
     store_location_for(resource, params[:redirect_to])
     super
   end  


  def create
    self.resource = warden.authenticate!(auth_options)
    if resource.deactivated?
      set_flash_message(:notice, :reactivated) if is_flashing_format?
      resource.reactivate
    end
    sign_in(resource_name, resource)
    yield resource if block_given?
    respond_with resource, location: after_sign_in_path_for(resource)
  end  
end

This sounds like you need some controller specific code.

When a user clicks on the link, have the corresponding action do something like this:

    def image_action # Whatever the action name is
      if current_user # current user is given to you by devise
        # Do stuff that a logged in user would do
      else
        # redirect to login page
      end
    end

The idea being that you leave the logic out of the view, and instead either have the controller serve the logged in user the appropriate data / page or have an unauthenticated user redirected to the login page.

current_user is a method given to you by devise that evaluates either to the currently logged in user making the request or nil if the user is not logged in.

If you post your controller code, I'll file out more of my example code, I just didn't want to make it too specific at risk of being confusing.

Since you are already using before_action :authenticate_user! in your controller for validating logged_in users..you just need to override that method to redirect to whatever page you like eg

In your application_controller.rb ,

def authenticate_user!
  if user_signed_in?
    super
  else
    redirect_to whatever_path
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM