ruby-on-rails - Rails 3.1 为 Facebook 路由混淆设计 Oauth

标签 ruby-on-rails facebook oauth devise

跟随之后:

https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

我可以通过 Facebook 注册用户。但我正在努力定义自己的重定向。

显然,已经注册/登录 facebook 的现有用户重定向到我的应用程序并继续操作是完美的,但我感兴趣的是通过 facebook 新创建的用户的情况。

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

user.persisted 是什么意思? find_for_facebook_oauth 与设计维基页面规定的相同;即通过电子邮件找到用户并在存在时将其返回,或者如果不存在则使用 autogen 密码创建一个新用户。

但我需要它将新创建的用户重定向到他们设置密码的页面。我不希望保留 stub 密码,我希望用户立即看到一个屏幕,用于 a) 确认他们的姓名和 b) 确认他们的密码。

我在 views/devise/invitations/edit 中为接受邀请(通过 https://github.com/scambra/devise_invitable/ )的人实现了这样一个屏幕——如果它能工作的话,这将非常适合这个。

我应该在哪里添加重定向以及这种重定向采用什么格式?我发现上面的 facebook 方法很难解释。我不明白为什么它会重定向到新的用户注册 url - 用户已创建或存在,那么持久性与什么有关?

显然很困惑,所以帮助赞赏。 :)

谢谢,

戴夫

最佳答案

.persisted? 方法检查该记录是否保存在您的数据库中。这是检查用户是否成功找到或创建的方法。如果没有,它将重定向到 new_user_registration_url,因为注册不成功(允许用户重试)。

要根据用户是否是新用户进行重定向,您可以检查用户对象是否有一些标志表明他们是新用户。你提到了密码,所以这样的东西可以工作(未经测试):

if @user.persisted?
  flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
  if @user.password.exists?
    sign_in_and_redirect @user, :event => :authentication
  else
    sign_in @user
    redirect_to ______ #insert path to set password etc
  end
else
  session["devise.facebook_data"] = request.env["omniauth.auth"]
  redirect_to new_user_registration_url
end

另一种方法(如果您想获得更多微调),您可能需要自定义 after_sign_in_path_for(resource_or_scope),如 on the Devise wiki 所述

关于ruby-on-rails - Rails 3.1 为 Facebook 路由混淆设计 Oauth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211548/

相关文章:

ios - 在 Swift 代码中存储开发者的消费者 secret 是否安全?

ruby-on-rails - Ruby on Rails : Cucumber: how to reset the db after each scenario?

javascript - 匹配在 Ruby on Rails 和 JavaScript 中加密的签名 header

ruby-on-rails - 在开发环境中通过 Octopus gem 复制 Rails Postgresql

php - 在 Facebook 帖子中标记他人?

azure - MSAL/ADAL C++ 相当于获取访问 token

ruby-on-rails - rails PG::InvalidDatetimeFormat:错误:时间戳类型的输入语法无效

facebook - Session.ReauthorizeRequest 的替代品是什么?

php - 如何使用 Build Ubuntu 运行 HipHop/HHVM

django - 回调中使用了错误的 sessionID,但仅限于一台特定计算机