【问题标题】:sign_in_and_redirect after facebook auth is recalling itselffacebook auth 召回后的sign_in_and_redirect
【发布时间】:2020-09-23 11:53:18
【问题描述】:

我有 omniauth-facebook 注册/登录。登录后,我想将用户重定向到他们点击 auth 块的页面。我正在使用 sign_in_and_redirect 但它似乎正在调用最后一个 url,在这种情况下是

http://localhost:3000/auth/facebook?callback_url=localhost%2Fauth%2Ffacebook%2Fcallback

所以它一直在做: Redirected to http://localhost:3000/auth/facebook?callback_url=localhost%2Fauth%2Ffacebook%2Fcallback

它会一直循环调用这个回调,直到它崩溃。这是omniauth控制器:

   def facebook
     generic_callback( 'facebook' )
   end

   def generic_callback( provider )
    @identity = Identity.find_for_oauth env["omniauth.auth"]
    if @identity.user != nil
      @user = @identity.user || current_user
    else
      @user = User.from_omniauth(request.env["omniauth.auth"])
    end

    if @user.nil?
      @user = User.create( email: @identity.email || "" )
      @identity.update_attribute( :user_id, @user.id )
    end

    if @user.email.blank? && @identity.email
      @user.update_attribute( :email, @identity.email)
    end

    if @user.persisted?
      @identity.update_attribute( :user_id, @user.id )
      @user = User.find(@user.id)
      sign_in_and_redirect @user, event: :authentication
      return
    else
      session["devise.#{provider}_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
      return
    end
  end

设计助手:

  def sign_in_and_redirect(resource_or_scope, *args)
    options  = args.extract_options!
    scope    = Devise::Mapping.find_scope!(resource_or_scope)
    resource = args.last || resource_or_scope
    sign_in(scope, resource, options)
    redirect_to after_sign_in_path_for(resource)
  end

路线

  resources :users
  devise_for :users, controllers: { registrations: "users/registrations", sessions: "users/sessions",  :omniauth_callbacks => "users/omniauth_callbacks" }, path: '', path_names: { sign_in: 'sign_in', sign_out: 'logout', sign_up: 'sign_up'}
  match '/auth/:provider/callback', to: 'sessions#create', via: [:get, :post]

如何让它定期登录?

【问题讨论】:

  • 您能否提供更多背景信息?问题中省略了任何与设计相关的代码吗?也许您在ApplicationController 或其他地方覆盖了after_sign_in_path_for?我之所以问,是因为我在结构上与问题中描述的代码相同,并且它可以按预期开箱即用。
  • 谢谢!现在很清楚了。请参阅我的更新答案。

标签: ruby-on-rails devise omniauth-facebook


【解决方案1】:

好的,问题来了。此行创建递归:

match '/auth/:provider/callback', to: 'sessions#create', via: [:get, :post]

改成:

get 'auth/facebook/callback', to: 'users/omniauth_callbacks#facebook'

如果您稍微重构#generic_callback 方法以使用params[:provider] 而不是provider 参数,那么您将能够摆脱路由中omniauth 提供程序的硬编码:

get 'auth/:provider/callback', to: 'users/omniauth_callbacks#generic_callback'

澄清问题:

在问题users/omniauth_callbacks#generic_callback 中描述的实现中根本没有调用。相反,您尝试调用sessions#create,它用于使用应用程序凭据(通常是电子邮件和密码)登录用户,而在omniauth 情况下,应该以特定方式创建和验证用户(通过generic_callbacksign_in_and_redirect 在你的情况下)。

简单地说:

您不必在此处调用sessions#create 来创建用户会话,#generic_callback#sign_in_and_redirect 就可以做到这一点。

【讨论】:

  • 如何获得 2 条路径前。问题是用户单击“登录”并重定向到 FB,然后 FB 使用身份验证令牌将用户发送回页面。所以在omniauth控制器中,request.fullpath是http://localhost:3000/auth/facebook/callback?code=——如果用户需要登录才能继续,他们需要被重定向到他们所在的页面,如果他们只是登录,他们需要被重定向到仪表板路径
  • 你可以像stored_location_for(:user)这样在你的回调方法中检查存储的路径。 Devise 不会存储任何最后的 URL,包括与 Omniauth 相关的 URL。它仅在请求因身份验证错误等而失败时存储位置。
  • 感谢您的帮助,我更改了一些路由,发现我也在应用程序控制器中调用 after_sign_in,这导致了一些问题。在omniauth登录后它仍然没有正确重定向,但它也没有重新调用回调url,这是什么?大约。谢谢!
猜你喜欢
  • 2020-07-08
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 2018-03-20
  • 1970-01-01
  • 2019-08-26
  • 2017-03-10
相关资源
最近更新 更多