验证设计的用户访问Omniauth路线之前

问题描述 投票:2回答:3

我开发利用制定了我的用户记录和omniauth由用户拥有的记录,而不是使用典型的omniauth +设计为用户记录的应用程序。我试图设计出的用户身份验证添加到omniauth路线/auth/:provider使非注册游客可以不针对特定的提供商访问这些路由并触发身份验证过程。

我已经能够通过使用色器件的authenticate_user!辅助方法,在我的会话控制器,以验证添加到回调,所以我至少未登记访客能够从omniauth流创建记录停止,但我想必须在omniauth流程的所有阶段色器件的用户身份验证工作。

关于如何设计的用户身份验证添加到初始omniauth路由是否使用使用类似的东西我目前的解决方案,或通过我的routes.rb文件制定的authenticate :user do任何想法?

ruby-on-rails ruby authentication devise omniauth
3个回答
0
投票

该解决方案为我工作。它本质上创建了检查,如果用户当前登录的新路线,然后将其重定向至要求的供应商的授权路径。

routes.rb中:

scope module: :authentication do
  devise_scope :user do
    # ...
    get 'users/auth/:provider/setup', to: 'omniauth_callbacks#before_request_phase'
  end
  # ...
end

omn​​iauth_callbacks_controller.rb:

def before_request_phase
  authorize_path = send("user_#{params[:provider]}_omniauth_authorize_path".to_s)
  if current_user.present?
    redirect_to(authorize_path)
  else
    redirect_to login_path(return_to: authorize_path)
  end
end

0
投票

一种解决方案的人用同样的问题来到这里:

不要在request_phase的设计认证。如果您使用的是战略的宝石,例如,omniauth Facebook的,你可以猴子补丁,在你的具体策略初始化,例如,在config/initializers/omniauth.rb

module FacebookExtension
  include Devise::Controllers::Helpers
  def request_phase
    # it might be 'admin_user_signed_in?' depends on what your user model is
    if user_signed_in? 
      super
    else
      # redirect to any path you think is suitable once devise authentication fail
      redirect Rails.application.routes.url_helpers.new_admin_user_session_path
    end 
  end  
end
OmniAuth::Strategies::Facebook.prepend FacebookExtension

-1
投票

添加在应用控制器的色器件autheticate_user!。或使用before filter之前omniauth认证方法来调用它。

© www.soinside.com 2019 - 2024. All rights reserved.