问题用铁轨上的红宝石注册到facebook

问题描述 投票:1回答:1

当我使用facebook注册时,我的网址是http://localhost:3000/registration然后它将更改为http://localhost:3000/registration#=但它不会让我登录它唯一的东西它在该页面上更改只是网址。

Contoller

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
  # 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

  def failure
    redirect_to root_path
  end
end

User

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, 
         :rememberable, :trackable, 
         :validatable, :confirmable, :omniauthable
  validates :fullname, presence: true, length: {maximum: 50}

  def self.from_omniauth(auth) 
    user = User.where(email: auth.info.email).first

    if user
      return user
    else
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.email = auth.info.email
        user.password = Devise.friendly_token[0,20]
        user.fullname = auth.info.name  
        user.image = auth.info.image
        user.uid = auth.uid
        user.provider = auth.provider
        # If you are using confirmable and the provider(s) you use validate emails,
        # uncomment the line below to skip the confirmation emails.
        user.skip_confirmation!
      end
    end
  end
end
ruby-on-rails ruby facebook facebook-graph-api rubygems
1个回答
0
投票

当您尝试创建用户时,您将获得:fullname和ofcourse的验证,您会收到验证错误和回滚。

尝试first_or_initialize而不是first_or_create,不要忘记save!用户。

where(provider: auth.provider, uid: auth.uid).first_or_initialize do |user|
        user.email = auth.info.email
        user.password = Devise.friendly_token[0,20]
        user.fullname = auth.info.name  
        user.image = auth.info.image
        user.uid = auth.uid
        user.provider = auth.provider
        # If you are using confirmable and the provider(s) you use validate emails,
        # uncomment the line below to skip the confirmation emails.
        user.skip_confirmation!
        user.save!
      end

P.Sazxswpoi这条线是可疑的。我不明白uid和提供商将如何找到用户,如果找到了,为什么要改变他的属性?例如,您有一个用户,此用户未通过电子邮件查找,但由uid和提供商查找。您更改了他的电子邮件,密码,然后用户无法通过电子邮件进行授权。

也许你必须创建新的模型where(provider: auth.provider, uid: auth.uid)将属于用户并通过授权查找用户。在这种情况下,您可以为用户添加更多提供程序。

authorization

class Authorization < ApplicationRecord
  belongs_to :user
end
© www.soinside.com 2019 - 2024. All rights reserved.