Devise + Twitter OmniAuth获取用户电子邮件 [英] Devise + Twitter OmniAuth Get user Email

查看:80
本文介绍了Devise + Twitter OmniAuth获取用户电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been using Devise + OmniAuth Twitter to authenticate the user to my portal. I am currently facing two issues.

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

  1. When the user is accessing /users/sign_up, the form is publicly visible. Instead, I want to redirect him to the Twitter authentication page.

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

When the user is accessing /users/sign_up, the email form is visible. I'm using this form to get the email address of the users after he signs up successfully from Twitter.

有人可以通过直接访问表单的人来帮助我解决此问题吗?

Can someone please help me solve this issue from people accessing the forms directly?

添加代码段:

 #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]

当用户授权Twitter与该应用共享您的信息时,所有用户的信息都将显示在哈希request.env["omniauth.auth"]中.

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"].

有关此哈希的更多详细信息,请参见文档.

See the documentation for more detail about this hash.

此处

这篇关于Devise + Twitter OmniAuth获取用户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆