【问题标题】:Devise + Omniauth multiple models设计 + Omniauth 多种模型
【发布时间】:2016-03-23 09:47:06
【问题描述】:

我在这里看到了一些解决方案,但没有一个解决我的问题。我使用 devise 生成了两个模型,即:User 和 Designer。我需要使用 Omniauth 分别为这两个模型注册/登录。目前这是我所拥有的:

用户.rb

def self.from_omniauth(auth)
   where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
   user.provider = auth.provider
   user.uid = auth.uid
   user.user_name = auth.info.name
   user.email = auth.info.email
   user.password = "password"
   user.skip_confirmation!
 end

user_authentications_controller.rb

class UserAuthenticationsController < Devise::OmniauthCallbacksController

 def create
    begin
        @user = User.from_omniauth(request.env['omniauth.auth'])
        sign_in_and_redirect @user
        #redirect_to root_url, notice: "Signed in!"
        flash[:success] = "Welcome, #{@user.first_name}!"
        UserMailer.welcome(@user).deliver_now
      rescue
        flash[:warning] = "There was an error while trying to authenticate you..."
      end
  end
 end

设计师.rb

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |designer|
   designer.provider = auth.provider
   designer.uid = auth.uid
   designer.user_name = auth.info.name
   designer.email = auth.info.email
   designer.password = "password"
   designer.skip_confirmation!
  end
 end

designer_authentication_controller.rb

DesignerAuthenticationsController < Devise::OmniauthCallbacksController


  def create
    begin
        @designer = Designer.from_omniauth(request.env['omniauth.auth'])
        sign_in_and_redirect @designer
        #redirect_to root_url, notice: "Signed in!"
        flash[:success] = "Welcome, #{@designer.first_name}!"
        UserMailer.welcome(@designer).deliver_now
      rescue
        flash[:warning] = "There was an error while trying to authenticate you..."
      end
  end
end

routes.rb

devise_scope :user do get "/auth/:provider/callback" => "user_authentications#create" end
devise_scope :designer do get "/auth/:provider/callback" => "designer_authentications#create" end

我的问题是:

1)无论我从哪个页面注册,设计师或用户,它都会以用户身份注册。我知道这是因为它同时使用 user_authentications_controller 进行注册。有什么想法可以让他们确定注册页面将调用哪个控制器?

2) 我的方法是否正确,或者有没有更好的方法来注册多个模型?

谢谢。请帮忙!

【问题讨论】:

    标签: ruby-on-rails ruby devise omniauth


    【解决方案1】:

    让我在这里分享一下我的经验,我做过和你现在做的一样的事情。问题可能是您没有为这两个用户执行所有步骤。

    以下是要遵循的步骤摘要 -

    1) rails generate devise MODEL (user and designer)
    
    2) rails generate devise:controllers [scope] (users and designers)
    
    3) rails generate devise:views USER (users and deigners)
    

    在此之后,做一个 rake 路线,看看设计如何为用户和设计师创建不同的路线,并相应地调用它。

    据我所知,最好使用角色而不是创建不同的用户,因为以后代码库会变得非常混乱。如果您刚刚开始构建一个,角色是前进的道路。

    【讨论】:

      【解决方案2】:

      为什么需要两个模型?您可以使用相同的身份验证,然后通过角色区分用户或设计者。我会看一些 railscasts 视频,以了解那里的不同技术。

      cancan 的作用: http://railscasts.com/episodes/192-authorization-with-cancan

      观看这个以了解如何编写自己的 Auth: http://railscasts.com/episodes/250-authentication-from-scratch-revised

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-07
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 2014-05-23
        • 2012-03-05
        • 2014-10-10
        • 1970-01-01
        相关资源
        最近更新 更多