Devise 和 OmniAuth:如何保存 Facebook 数据以供以后使用?

发布于 2024-11-26 17:30:56 字数 1937 浏览 1 评论 0原文

我已经使用 Devise 和 OmniAuth(内置于 Devise 中)成功实现了 Facebook 登录。现在我需要弄清楚如何将用户的名称存储在数据库中,以便我可以向其他用户显示他们的名称,以查看他们在我的应用程序中创建/编辑的记录。

我一定是从某个地方复制了这段代码,它看起来应该可以工作,但它绝对没有将名称保存在数据库中:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
      @user = User.find_for_facebook_oauth(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"] = env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
end

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

   def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token['extra']['user_hash']
    if user = User.find_by_email(data['email'])
      if user.first_name != data['first_name']
        user.update_attributes :first_name => data['first_name']
      end
      user
    else # Create an user with a stub password. 
      User.create!(:email => data['email'], 
                   :first_name => data['first_name'],
                   :password => Devise.friendly_token[0,20]) 
    end
  end

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

我做错了什么?

I have successfully implemented login with Facebook using Devise and OmniAuth (built into Devise). Now I need to figure out how to store the user's name in the database so that I can display their name for other users to see that they created/edited records in my application.

I must have copied this code from somewhere, and it looks like it should be working, but it is definitely not saving the name in the database:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
      @user = User.find_for_facebook_oauth(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"] = env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
end

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

   def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token['extra']['user_hash']
    if user = User.find_by_email(data['email'])
      if user.first_name != data['first_name']
        user.update_attributes :first_name => data['first_name']
      end
      user
    else # Create an user with a stub password. 
      User.create!(:email => data['email'], 
                   :first_name => data['first_name'],
                   :password => Devise.friendly_token[0,20]) 
    end
  end

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

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-12-03 17:30:56

Andrew,如此处所写:attr_accessible 方法使属性可用于质量任务。

你应该这样说:

attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name

Andrew, as written here: the method attr_accessible makes the attribute available for mass assignment.

You should put it like this:

attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文