使用Twitter、Omniauth和designe实现登录

使用Twitter、Omniauth和designe实现登录,twitter,devise,omniauth,Twitter,Devise,Omniauth,我指的是使用twitter、omniauth和Desive实现登录的railscasts: 场景:一个twitter用户来到我的应用程序。单击Twitter链接以登录,并转到/auth/Twitter。他允许我在twitter网站上使用我的应用程序,并被重定向回我的应用程序。他必须在我的网站上输入他的电子邮件id。他使用该网站,然后退出我的应用程序。然后他想再次登录。他再次点击Twitter登录图标。 我的问题:我希望他不必再次在twitter上授权我的应用程序。但事实并非如此。这一次,tw

我指的是使用twitter、omniauth和Desive实现登录的railscasts:

  • 场景:一个twitter用户来到我的应用程序。单击Twitter链接以登录,并转到/auth/Twitter。他允许我在twitter网站上使用我的应用程序,并被重定向回我的应用程序。他必须在我的网站上输入他的电子邮件id。他使用该网站,然后退出我的应用程序。然后他想再次登录。他再次点击Twitter登录图标。 我的问题:我希望他不必再次在twitter上授权我的应用程序。但事实并非如此。这一次,twitter再次要求该用户授权

    我的代码完全遵循railscasts:

     <routes.rb>
     match '/auth/:provider/callback' => 'authentications#create'
    
    
    class AuthenticationsController < ApplicationController
    ...
    def create
        omniauth = request.env["omniauth.auth"]
    
        authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    
        if authentication
          flash[:notice] = "Signed in successfully."
          sign_in_and_redirect(:user, authentication.user)
        elsif current_user
          current_user.authentications.create(:provider => omniauth['provider'], :uid => omniauth['uid'])
          flash[:notice] = "Authentication successful."
          redirect_to authentications_url
        else
          user = User.new
          user.apply_omniauth(omniauth)
          if user.save
            flash[:notice] = "Signed in successfully."
            sign_in_and_redirect(:user, user)
          else
            session[:omniauth] = omniauth.except('extra')
            redirect_to new_user_registration_url
          end
        end
      end
    ...
    end
    
    
    匹配'/auth/:提供程序/回调'=>'身份验证#创建'
    类身份验证控制器<应用程序控制器
    ...
    def创建
    omniauth=request.env[“omniauth.auth”]
    身份验证=身份验证。通过提供程序和uid查找(omniauth['provider'],omniauth['uid'])
    如果验证
    flash[:notice]=“已成功登录。”
    登录并重定向(:user,authentication.user)
    当前用户
    当前用户.authentications.create(:provider=>omniauth['provider'],:uid=>omniauth['uid'])
    flash[:notice]=“身份验证成功。”
    将\u重定向到身份验证\u url
    其他的
    user=user.new
    user.apply\u omniauth(omniauth)
    如果user.save
    flash[:notice]=“已成功登录。”
    登录和重定向(:用户,用户)
    其他的
    会话[:omniauth]=omniauth.except('extra')
    将\重定向到新\用户\注册\ url
    结束
    结束
    结束
    ...
    结束
    
    Desive的用户模型修改为:

    class User < ActiveRecord::Base
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable
    
      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me
      has_many :authentications
    
        def apply_omniauth(omniauth)
            authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
        end
    
        def password_required?
            (authentications.empty? || !password.blank?) && super
        end
    end
    
    class用户omniauth['provider'],:uid=>omniauth['uid'])
    结束
    是否需要def密码?
    (authentications.empty?| |!password.blank?&&super)
    结束
    结束
    

    我需要做些什么来确保返回的用户在每次重定向到/auth/twitter???时不会被要求授权我的应用程序?

    如果您遵循ryan bates关于Desive和omniauth的内容,您的用户模型应该在用户和身份验证之间建立关联

    用户模型需要如下内容: 有很多:身份验证

    您的routes.rb文件还需要ressource:

    匹配'/auth/:提供程序/回调'=>'身份验证#创建'

    为:用户,:控制器=>{:会话=>'sessions',:注册=>'registrations}设计

    资源:身份验证

    希望这会有所帮助

    致意
    Jan

    我用这个答案解决了这个问题:


    您必须添加客户端选项散列。

    我的omniauth初始值设定项如下所示:“Rails.application.config.middleware.use omniauth::Builder do provider:twitter,***************************************************************************************************end”