【问题标题】:Devise + Twitter OmniAuth Get user Email设计 + Twitter OmniAuth 获取用户电子邮件
【发布时间】:2014-12-16 12:40:09
【问题描述】:

我一直在使用 Devise + OmniAuth Twitter 向我的门户网站验证用户身份。我目前面临两个问题。

  1. 当用户访问 /users/sign_up 时,表单是公开可见的。相反,我想将他重定向到 Twitter 身份验证页面。

  2. 当用户访问 /users/sign_up 时,电子邮件表单是可见的。他从 Twitter 成功注册后,我正在使用此表单获取用户的电子邮件地址。

有人可以帮我解决直接访问表单的人的这个问题吗?

添加代码片段:

 #config/routes.rb
  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

  devise_scope :user do
    get "skcript1625" => "devise/sessions#new", as: :login
    get "logout", to: "devise/sessions#destroy", as: :logout
  end



# app/models/user.rb
 devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable
 def self.from_omniauth(auth)
        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.name = auth.info.name   # assuming the user model has a name
          user.profileimg = auth.info.profileimg # assuming the user model has an image
        end
      end

【问题讨论】:

  • 请注意,twitter 不会在 auth hash 中提供用户的电子邮件。

标签: ruby-on-rails twitter devise omniauth


【解决方案1】:

您必须使用以下链接重定向用户

<%= link_to "Sign in with Twitter", user_omniauth_authorize_path(:twitter) %>

确保你告诉你的模型(通常是“用户”)它是“omniauthable”

devise :omniauthable, :omniauth_providers => [:twitter]

当用户授权 twitter 与应用程序共享您的信息时,所有用户的信息都在哈希 request.env["omniauth.auth"] 中可用。

有关此哈希的更多详细信息,请参阅documentation。

编辑:一切都很好解释here

【讨论】:

  • 我已经完成了@doum 的所有工作,即便如此,它仍然可以访问。这就是为什么我很困惑。用一些代码更新问题。
  • 当他输入 url /user/sing_up 时,只需重定向用户(js 或在你的 rails 控制器中)。那行不通?
猜你喜欢
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
  • 2013-04-01
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
相关资源
最近更新 更多