简体   繁体   English

Devise + Twitter OmniAuth获取用户电子邮件

[英]Devise + Twitter OmniAuth Get user Email

I've been using Devise + OmniAuth Twitter to authenticate the user to my portal. 我一直在使用Devise + OmniAuth Twitter验证用户到我的门户的身份。 I am currently facing two issues. 我目前面临两个问题。

  1. When the user is accessing /users/sign_up, the form is publicly visible. 当用户访问/ users / sign_up时,该表单是公开可见的。 Instead, I want to redirect him to the Twitter authentication page. 相反,我想将他重定向到Twitter身份验证页面。

  2. When the user is accessing /users/sign_up, the email form is visible. 当用户访问/ users / sign_up时,将显示电子邮件表单。 I'm using this form to get the email address of the users after he signs up successfully from Twitter. 在他从Twitter成功注册后,我正在使用此表单获取用户的电子邮件地址。

Can someone please help me solve this issue from people accessing the forms directly? 有人可以通过直接访问表单的人来帮助我解决此问题吗?

Adding Code Snippets: 添加代码段:

 #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

You have to redirect the user with the following link 您必须使用以下链接重定向用户

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

Make sure you told your model (usually 'user') that it is 'omniauthable' 确保您告诉模型(通常是“用户”)它是“全能的”

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

When the user authorized twitter to share your info with the app, all the user's information is available in a hash request.env["omniauth.auth"] . 当用户授权Twitter与该应用共享您的信息时,所有用户的信息都可以在hash request.env["omniauth.auth"]

See the documentation for more detail about this hash. 有关此哈希的更多详细信息,请参见文档

Edit: Everything is well explained here 编辑:一切都很好解释在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM