简体   繁体   English

如何在Rails的AJAX调用中检查用户是否已通过Devise进行身份验证?

[英]How to check if a user is authenticated with Devise on an AJAX call in rails?

I have a button that allows a person to join a group (called a pod in my application). 我有一个按钮,允许一个人加入一个组(在我的应用程序中称为“ pod”)。 The button uses remote: true to do an AJAX call. 该按钮使用remote:true进行AJAX调用。 I have a join table that manages the relationships between users and pods with has_many: though:. 我有一个联接表,用于使用has_many:尽管管理用户和pod之间的关系。 In the join table controller I want to be able to check if the user is authenticated before that user can join a pod. 在联接表控制器中,我希望能够在该用户可以加入Pod之前检查该用户是否已通过身份验证。 If not authenticated, the user should be taken to a log in page and then back to the page with all the pods listed. 如果未通过身份验证,则应将用户带到登录页面,然后返回列出了所有Pod的页面。

Right now, I've got the following code in pod_users_controller: 现在,我在pod_users_controller中有以下代码:

  before_action :user_logged_in

  ...

  private

  def user_logged_in
      render :js => "window.location = '#{new_user_session_path}'" unless user_signed_in?
  end

In my application controller I have the following code to redirect the user back to the page they were on before authenticating: 在我的应用程序控制器中,我有以下代码将用户重定向回验证之前的页面:

  def store_location
     session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/ 
  end

  def after_sign_in_path_for(resource)
      session[:previous_url] || root_path
  end

It's kind of works but it feels very flaky. 这是一种作品,但感觉非常片状。 For example, I have another button which allows you to create a new group with an AJAX bootstrap modal. 例如,我有另一个按钮,可让您使用AJAX引导程序模式创建新组。 I've tried using the same method with before_action :user_logged_in in the pods_controller file. 我已经尝试过使用与pods_controller文件中的before_action:user_logged_in相同的方法。 It pops the modal without the form and then redirects. 它弹出不带形式的模态,然后重定向。 Or when I click on the join button it will redirect me to the form to create a new pod if I just clicked the "start a pod" button before that. 或者,当我单击加入按钮时,如果在此之前单击“启动容器”按钮,它将把我重定向到表单以创建一个新容器。

My question is, is there a better way to do what I'm doing? 我的问题是,有没有更好的方法来做我在做的事情? How do I redirect to login before the modal pops? 如何在弹出弹出窗口之前重定向到登录名? I'm just learning ruby and rails so any best practices would be much appreciated. 我只是在学习红宝石和铁轨,所以任何最佳实践将不胜感激。 Also, this is a total side question but in this approach I have the same method I'm calling "user_logged_in" from two controllers. 同样,这是一个附带的问题,但是在这种方法中,我有相同的方法,即从两个控制器中调用“ user_logged_in”。 Right now I have that method at the bottom of both. 现在,我在两种方法的底部都有该方法。 I'm assuming there's a place I can put it to make it accessible by multiple controllers from one place? 我假设有一个地方可以放置它,以便多个控制器从一个地方可以访问它?

Thank you! 谢谢!

I'm on Rails 4.2 and Devise 3.4 我在Rails 4.2和Devise 3.4上

Devise comes with these helpers: https://github.com/plataformatec/devise#controller-filters-and-helpers Devise带有以下帮助器: https : //github.com/plataformatec/devise#controller-filters-and-helpers

That includes the before_action :authenticate_user! 其中包括before_action :authenticate_user! as well as the user_signed_in? 以及user_signed_in? helper (almost the same name as yours), which is available throughout the app. 帮助程序(几乎与您的名字相同),可在整个应用程序中使用。 You could use user_signed_in? 您可以使用user_signed_in? to redirect from within a view with ERB or in a controller. 从带有ERB的视图中或在控制器中重定向。

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

相关问题 Rails 4 devise-如果未通过身份验证,则重定向用户 - Rails 4 devise - redirect user if not authenticated Rails设计Ajax调用 - Rails Devise Ajax call Rails Devise将经过身份验证的用户重定向到特定路径 - Rails Devise redirecting authenticated user to a specific path 如果用户已经通过身份验证,如何允许设计登录? - How to allow sign in with devise if user is already authenticated? 如何:当用户无法通过身份验证时重定向到特定页面(设计 rails gem) - How To: Redirect to a specific page when the user can not be authenticated (devise rails gem) Rails 3 w / Devise:如何根据用户是否经过身份验证设置两个单独的主页? - Rails 3 w/ Devise: How to set two separate homepages based on whether the user is authenticated or not? 用户未经过身份验证时重新路由自定义路由(Devise,rails gem) - Rerouting custom routes when user is not authenticated (Devise, rails gem) Rails4 + Devise:用户未通过身份验证时“隐藏”路线 - Rails4+Devise: “hide” routes when user is not authenticated Rails + Devise:如何在登录时检查是否记住了用户 - Rails + Devise: How can I check if a user is remembered when signing in 如何检查用户是否使用 rails devise 登录? - How can I check if user is signed in with rails devise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM