简体   繁体   English

设计-如何触发页面上的登录用户已经可以访问

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

I am using Devise 3.5.2. 我正在使用Devise 3.5.2。 I have an Item model with a show action that anyone can access( before_filter :authenticate_user!, except: :show ). 我有一个带有show动作的Item模型,任何人都可以访问( before_filter :authenticate_user!, except: :show )。 On the show page I have a button that triggers a file upload dialog using jquery. 在显示页面上,我有一个按钮,该按钮使用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. 编辑:我尝试在此链接https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in,但似乎没有工作。

My sessions controller looks like this. 我的会话控制器看起来像这样。 Maybe my create method is causing a conflict? 也许我的create方法引起冲突?

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. current_user是由devise提供给您的方法,该方法将评估当前发出请求的当前登录用户,如果该用户未登录,则为nil

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! 由于您已经在使用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 , 在您的application_controller.rb

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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