【问题标题】:Rails - Generate email address when one doesn't exist with Omniauth integrationRails - 当 Omniauth 集成不存在时生成电子邮件地址
【发布时间】:2013-07-10 20:06:12
【问题描述】:

我正在使用带有Rails 的omniauth 并尝试连接twitter、facebook 和google 以进行身份​​验证,但一直遇到此错误:

PG::Error: ERROR:  duplicate key value violates unique constraint "index_users_on_email"
DETAIL:  Key (email)=() already exists.

这是我的身份验证控制器:

class AuthorizationsController < ApplicationController


      def create
        authentication = Authorization.find_by_provider_and_uid(auth['provider'], auth['uid'])

        if authentication
          flash[:notice] = "Signed In Successfully"
          sign_in authentication.user, event: :authentication
          redirect_to root_path
        else
          athlete = Athlete.new
          athlete.apply_omniauth(auth)

          debugger

          if athlete.save(validate: false)
            flash[:notice] = "Account created and signed in successfully"
            sign_in athlete, event: :authentication
            redirect_to finalize_profile_path
          else
            flash[:error] = "An error has occurred. Please try again."
            redirect_to root_path
          end
        end
      end

      def failure
        render json: params.to_json
      end

      private

        def auth
          request.env["omniauth.auth"]
        end

        def resource(user_type)
          user_type.downcase.to_sym
        end

    end

我认为正在发生的事情是,当创建运动员时,它正在创建一个带有空白电子邮件地址并且唯一密钥失败......我该如何解决这个问题?我想我知道如何为 Google 集成解决此问题,但由于 Twitter 不返回电子邮件,因此此问题不会自行解决

【问题讨论】:

    标签: ruby-on-rails postgresql omniauth rails-postgresql


    【解决方案1】:

    这就是我能够让它工作的方式:

    class AuthorizationsController < ApplicationController
    
      def create
        authentication = Authorization.find_by_provider_and_uid(auth['provider'], auth['uid'])
    
        if authentication
          flash[:notice] = "Signed In Successfully"
          sign_in authentication.user, event: :authentication
          redirect_to root_path
        else
          athlete = Athlete.new(email: generate_auth_email(params[:provider]) )
          athlete.apply_omniauth(auth)
    
          debugger
    
          if athlete.save(validate: false)
            flash[:notice] = "Account created and signed in successfully"
            sign_in athlete, event: :authentication
            redirect_to finalize_profile_path
          else
            flash[:error] = "An error has occurred. Please try again."
            redirect_to root_path
          end
        end
      end
    
      def failure
        render json: params.to_json
      end
    
      private
    
        def auth
          request.env["omniauth.auth"]
        end
    
        def resource(user_type)
          user_type.downcase.to_sym
        end
    
        def generate_auth_email(provider)
          return auth.info.try(:email) unless provider == "twitter"
          return "#{auth.uid}@twitter.com" if provider == "twitter"
        end
    
    end
    

    我使用 twitter.com 作为域的 twitter uid 创建电子邮件,因为 twitter 不返回电子邮件地址

    希望这对未来的人有所帮助

    【讨论】:

    • 很好。但是比需要的要复杂得多。你可以这样做:`user.email = auth.info.email = "#{auth.uid}@twitter.com" `
    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 2020-08-16
    • 2021-10-18
    • 2010-10-13
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多