3

即使omniauth控制器确认登录成功,我似乎也无法从我的数据库中保存用户。我遵循了omniauth wiki的说明:https ://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

用户.rb:

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :encrypted_password, :provider, :uid
attr_accessor :email, :password, :password_confirmation, :remember_me, :provider,   :uid, :name

def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.encrypted_password = Devise.friendly_token[0,20]
user.save!
end
end



def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  user = User.where(:provider => auth.provider, :uid => auth.uid).first
  unless user
    user = User.create(
                         provider:auth.provider,
                         uid:auth.uid,
                         email:auth.info.email,
                         encrypted_password:Devise.friendly_token[0,20]
                         )
  #user.ensure_authentication_token!
  #added extra to create authentication for user
  user.save
  end
  user
end

def self.new_with_session(params, session)
super.tap do |user|
  if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
    user.email = data["email"] if user.email.blank?
  end
end
end
end

omn​​iauth_callback_controller.rb:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
# Or alternatively,
# raise ActionController::RoutingError.new('Not Found')
end

def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
#@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
  redirect_to messages_url
end
end
end

我尝试使用默认设计控制器登录,它通过用户数据库。由于我无法存储到数据库,我也无法获取 uid。

4

2 回答 2

1

通过从不同的方法创建一个新的应用程序:http: //supriya-surve.blogspot.com/2012/06/rails-3-devise-omniauth-facebook.html,我找到了解决方案。

更新 user.rb find_for_facebook_auth:

    def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  user = User.where(:provider => auth.provider, :uid => auth.uid).first
  unless user
    user = User.create(
                         provider:auth.provider,
                         uid:auth.uid,
                         email:auth.info.email,
                         password:Devise.friendly_token[0,20]
                         )

  end
  user
end

然后user.rb的attr_accessible:

    attr_accessible :email, :password, :password_confirmation, :remember_me, :provider, :uid, :oauth_token, :oauth_expires_at

成功登录后,用户被保存到数据库中。

于 2012-11-15T11:39:14.193 回答
0

当我决定将 Tumblr 身份验证添加到现有的 Devise 用户模型时,我也遇到了同样的问题。事实证明,Devise 在index_users_on_email数据库索引上抛出了一个错误。

网上没有人有这个问题,所以我想我会把它扔在那里。我花了很长时间试图弄清楚。很高兴你解决了:)

于 2013-08-12T14:21:04.867 回答