Devise - 多个用户的单一登录表单 [英] Devise - single sign in form for multiple users

查看:142
本文介绍了Devise - 多个用户的单一登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Devise模型,用户和业务;我希望两者能够使用单一登录表单登录。我使用骨干js,我有一个定制的视图,所以视图不是一个问题。 ajax请求用于登录,它对用户的预期而言是适用的,但不适用于企业。



我已经搜索过Google,并提出了一些提到使用STI的解决方案解决这个问题,但是这个项目做得很好,现在我不能做这样的改变。我正在考虑重写Devise会话控制器的方式:


  1. 检查给定的电子邮件地址是否是用户,然后验证用户使用Warden。

  2. 如果没有发现该电子邮件的用户,则使用Warden对商业模式进行身份验证。


$ b $我不知道如何改变代码以达到上述目的,不知道监听器的工作原理以及我需要调整哪些参数以达到上述目的,需要调用哪些功能。任何人都可以指向正确的方向,或提供一个如何向前推进的例子。



谢谢。

解决方案

不要担心,你不会卡住。



我认为你最好的选择是覆盖Devise的会话控制器并修改一个会话的'new'和'create'方法。



所以在你自己的sessions_controller.rb中,你会有这样的:

  class SessionsController< Devise :: SessionsController 
#GET / resource / sign_in
def new
resource = build_resource(nil,:unsafe => true)
clean_up_passwords(resource)
respond_with (resource,serialize_options(resource))
end

#POST / resource / sign_in
def create
resource = warden.authenticate!(auth_options)
set_flash_message(:notice,:signed_in)if is_navigational_format?
sign_in(resource_name,resource)
respond_with resource,:location => after_sign_in_path_for(资源)
end
end

在你的路线上,你会使用类似的东西(取决于你所命名的用户模型):

  devise_for:users,:controllers => {
:sessions => '会话'
}


devise_for:企业,控制器=> {
:sessions => '会话'
}

上面的会话控制器根本没有自定义。我不知道你的代码,所以我真的不能帮助那里。但是基本步骤是:


  1. 检查 auth_options resource_name 变量,以了解它们如何存储数据。

  2. 如果资源在用户表中找不到。

Devise是伟大的,角色很棒,但有时拥有一个用户模型,甚至使用STI,没有意义。因为我在最近的一个项目中处理了这个问题,所以我在这个确切的问题上写了一个更长的文章。


I have two Devise models, User and Business; I would like both to be able to sign in using a single sign in form. I am using backbone js and I have a customized view so the view is not a concern. An ajax request is used for the login, it works for users as expected but not for businesses.

I have searched google and come across a few solutions that mention using STI to solve this, but the project is very much done and I can't make such a change now. I was thinking along the lines of overriding the Devise sessions controller and:

  1. Check if the given email address is a User, then authenticate the user using Warden.
  2. If no user with that email is found then authenticate with the Business model using Warden.

I can't figure out how to change the code to achieve the above, I don't know how warden works and which params I need to tweak to achieve the above, what functions need to be called. Can anyone point me in the right direction or provide an example of how should I move forward with this.

Thanks.

解决方案

Don't worry, you're not stuck.

I think your best option is to override Devise's session controller and alter the 'new' and 'create' methods for a session.

So in your own "sessions_controller.rb", you will have something like this:

class SessionsController < Devise::SessionsController
  # GET /resource/sign_in
  def new
    resource = build_resource(nil, :unsafe => true)
    clean_up_passwords(resource)
    respond_with(resource, serialize_options(resource))
  end

  # POST /resource/sign_in
  def create
    resource = warden.authenticate!(auth_options)
    set_flash_message(:notice, :signed_in) if is_navigational_format?
    sign_in(resource_name, resource)
    respond_with resource, :location => after_sign_in_path_for(resource)
  end
end

And in your routes, you would use something like (depending on what you named your user models):

devise_for :users, :controllers => {
    :sessions => 'sessions'
}


devise_for :businesses, :controllers => {
    :sessions => 'sessions'
}

The above session controller is not customized at all. I don't know your code, so I can't really help there. But the basic steps are:

  1. Inspect the auth_options and resource_name variables to understand how they are storing data.
  2. Add your conditional logic to alter those variables if the resource isn't found in the Users table.

Devise is great, and Roles are great, but sometimes having one User model, or even using STI, don't make sense. I'm writing a longer post on this exact issue soon, since I dealt with it in a recent project.

这篇关于Devise - 多个用户的单一登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆