Rails 3.1设计Oauth为Facebook路由混乱

Dav*_*ave 5 facebook ruby-on-rails 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
Run Code Online (Sandbox Code Playgroud)

user.persisted是什么意思?find_for_facebook_oauth与设计维基页面规定的相同; 即通过电子邮件查找用户并返回(如果存在),或者如果不存在则创建具有自动密码的新用户.

但我需要它将新创建的用户重定向到他们设置密码的页面.我不希望存根密码持久存在,我希望用户立即出现一个屏幕,以确认他们的名字和b)确认他们的密码.

我为接受邀请(通过https://github.com/scambra/devise_invitable/)的人提供了这样的屏幕,这些邀请在视图/设计/邀请/编辑中 - 如果它可以工作,这将非常适合这个.

我应该在哪里添加重定向以及这种重定向采用什么格式?我觉得上面的facebook方法很难解释.我无法理解为什么它会重定向到新的用户注册网址 - 用户被创建或存在,那么持久性与什么有关?

显然困惑,所以帮助升值.:)

谢谢,

戴夫

Mat*_*ick 9

.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
Run Code Online (Sandbox Code Playgroud)

after_sign_in_path_for(resource_or_scope)Devise wiki所述,你可能想要自定义一个替代方案(如果你想得到更多微调)