为已注册的用户集成facebook登录

时间:2015-07-18 04:56:43

标签: ruby-on-rails devise omniauth

我正在使用Omniauth文档中的示例代码将facebook登录集成到我的应用程序中

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

def self.from_omniauth(hash)
  where(email: hash.info.email).first_or_create do |user|
    user.email = hash.info.email
    user.password = Devise.friendly_token[0,20]
    user.username = hash.info.name   # assuming the user model has a name
  end
end

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def facebook

    # render :text => request.env['omniauth.auth'].to_yaml

    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

我按照原样跟踪了示例。但是,由于omniauth_callbacks_controller中的代码我说如果用户是应用程序的新用户,那么它应该遵循else语句并被重定向到用户注册URL。但是,会发生的是始终在数据库中创建新用户。如果用户在我的应用中还没有帐户,我不应该落入else语句吗?

根据from_omniauth方法的建议实现,我了解到first_or_create实际上是使用来自facebook的信息在数据库中创建一个新用户。但如果是这样的话,那么使用else选项重定向到new_user_registration_url有什么用?

我还没有看到什么?

0 个答案:

没有答案
相关问题