简体   繁体   English

如何启用所有经过身份验证的路由以显示Devise ajax登录表单?

[英]How to enable all authenticated routes to show the Devise ajax Login form?

I have implemented devise to accept Ajax Login and have created a form to login via ajax which I have put up in a modal . 我已经实现了设计以接受Ajax登录,并创建了一个表单以通过ajax登录,而该表单是我放在模式中的 However, only when I set up event binders for specific buttons, which trigger the modal ( Such as " Login now Button ", am I am able to see the form and login. However, I want the modal to appear for any restricted route. ( For example, if I click on : " Add review " button, the modal should appear for login( If the user is unauthenticated ). How do I achieve this ? ) 但是,只有当我为触发模式的特定按钮设置事件绑定程序时(例如“ Login now Button ”,我才能看到表单并登录。但是,我希望模式出现在任何受限制的路线中。 (例如,如果我单击:“ 添加评论 ”按钮,则应该显示用于登录的模式(如果用户未经身份验证)。如何实现此目的?)

Can I add a specialized class to all the links which require authentication so that I can target them? 是否可以向所有需要身份验证的链接添加专门的类,以便我可以将它们定位为目标?

This sounds like you need a handler for your ajax authentication in your JS. 听起来您需要在JS中进行ajax身份验证的处理程序。

The problem you have is that Rails does not invoke JS unless you explicitly define it. 您遇到的问题是,除非您明确定义,否则Rails不会调用JS。 Especially with the likes of user authentication, your application will do nothing unless you tell it to: 尤其是对于用户身份验证之类的应用程序,除非您告知:

#app/assets/javascripts/application.js
$(document).on("ajax:error", function(xhr, status, error){
   if(xhr.status === "401") {
      // invoke modal
   }
});

Devise returns a 401 - Unauthorized error whenever you hit an action without the right credentials: 每当您在没有正确凭据的情况下执行操作时,Devise都会返回401 - Unauthorized错误:

在此处输入图片说明

This means that if you send an Ajax request (with remote: true etc), you'll be able to capture this response and use it to invoke the modal. 这意味着,如果您发送Ajax请求(带有remote: true等),则可以捕获此响应并使用它来调用模式。

I don't know how you're invoking your modal form; 我不知道您如何调用模态表格; the code above will give you the ability to put your own functionality into the function. 上面的代码将使您能够将自己的功能放入功能中。

-- -

We've done something similar (just click "login" at the top): 我们做了类似的事情(只需单击顶部的“登录”):

在此处输入图片说明

We had to change the Devise::SessionsController a little to get it working. 我们必须稍微修改一下Devise::SessionsController才能使其正常运行。 I can do the same if you need it; 如果您需要,我也可以这样做。 I think sending a standard request, as above, will work as reqired. 我认为,如上所述,发送标准请求将可以正常工作。


Update 更新

Okay this is a head-burner. 好吧,这真是令人头疼。

I'm sure there has to be a way to send the request to the server & evaulate the response. 我敢肯定,必须请求发送到服务器和evaulate响应的方式。

The most basic implementation of this would be to make every link go through ajax, although this would kill a lot of the Rails convention ... 最基本的实现是使每个链接都通过ajax,尽管这会杀死很多Rails约定 ...

#app/assets/javascripts/application.js
$(document).on("click", "a", function(e){
   e.preventDefault();
   // send via ajax & then monitor the response
});

I've been trying to find a way to fire the link, capture the response in JS and then show the modal if the response is 401 etc... 我一直试图找到一种方法来触发链接,捕获JS中的响应,然后显示模式(如果响应为401等)...

There has to be a way to do it. 必须有一种方法来做到这一点。 Let me keep checking 我继续检查

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

相关问题 如果未经过身份验证,则重定向所有路由以进行登录 - Redirect on all routes to login if not authenticated 如果用户已登录并且在所有其他情况下显示公共路由,如何重定向到 /dashboard 公共路由 /login 和 /register? - How to redirect to /dashboard public routes /login and /register if user is logged in and in all other cases show public routes? 设计ajax Facebook登录 - Devise ajax facebook login 没有从Rails 4上的Devise登录表单接收ajax事件 - Not receiving ajax events from Devise login form on Rails 4 在以下情况下,如何禁用和启用AJAX函数调用中表单上存在的所有具有相同类的按钮? - How to disable and enable all the buttons with same class present on form on AJAX function call in following scenario? 如何在 React Router 4 中实现经过身份验证的路由? - How to implement authenticated routes in React Router 4? 如何处理Ajax登录表单上的错误? - How to handle an error on ajax login form? 如何使用登录表单登录 django? 带或不带 Ajax - How to loging to django with login form? With or without Ajax 如果通过了Express / Node JS认证,则将用户数据放入所有路由 - putting User data in all routes if authenticated Express/Node JS 使用ajax和javascript登录表单 - login form with ajax and javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM