首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails5 5/Devise/Omniauth(多个供应商)

Rails5 5/Devise/Omniauth(多个供应商)
EN

Code Review用户
提问于 2017-07-18 14:23:18
回答 1查看 725关注 0票数 2

在过去的两天里,我花了大部分时间让它开始工作,但它终于起作用了,但我觉得它是极其多余的。

我正在为一个名为“授权”的表遵循一种belongs_to方法--我希望用户能够通过电子邮件注册,或者我实现的任何有电子邮件的Omniauth通道(我会给它一个设计密码),并自由地链接/未链接授权的应用程序。

Auth控制器

代码语言:javascript
运行
复制
class AuthenticationsController < ApplicationController
  skip_before_action :verify_authenticity_token, :only => :steam

  def index
    @authentications = current_user.authentications.all
  end
  # <%= link_to 'Authentications', authentications_path %>
  def home
  end

  def twitter
     omni = request.env["omniauth.auth"]
     authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])

     if authentication
      flash[:notice] = "Logged in Successfully"
      sign_in_and_redirect User.find(authentication.user_id)
     elsif current_user
      token = omni['credentials'].token
      token_secret = omni['credentials'].secret

      current_user.authentications.create!(:provider => omni['provider'], 
                                          :uid => omni['uid'], 
                                          :token => token, 
                                          :token_secret => token_secret)
      flash[:notice] = "Authentication successful."
      sign_in_and_redirect current_user
     else
      user = User.new
      user.apply_omniauth(omni)

      if user.save
         flash[:notice] = "Logged in."
         sign_in_and_redirect User.find(user.id)             
      else
         session[:omniauth] = omni.except('extra')
         redirect_to new_user_registration_path
      end
     end 
   end

   def destroy
     @authentication = Authentication.find(params[:id])
     @authentication.destroy
     redirect_to authentications_url, :notice => "Successfully destroyed authentication."
   end


   def facebook
     omni = request.env["omniauth.auth"]
     authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])

     if authentication
      flash[:notice] = "Logged in Successfully"
      sign_in_and_redirect User.find(authentication.user_id)
     elsif current_user
      token = omni['credentials'].token
      token_secret = ""

      current_user.authentications.create!(:provider => omni['provider'], 
                                           :uid => omni['uid'], 
                                           :token => token, 
                                           :token_secret => token_secret)

      flash[:notice] = "Authentication successful."
      sign_in_and_redirect current_user
     else
      user = User.new
      user.email = omni['extra']['raw_info'].email 

      user.apply_omniauth(omni)

      if user.save
         flash[:notice] = "Logged in."
         sign_in_and_redirect User.find(user.id)             
      else
         session[:omniauth] = omni.except('extra')
         redirect_to new_user_registration_path
      end
     end
   end

    def steam
        omni = request.env["omniauth.auth"]
        authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])

        if authentication
            flash[:notice] = "Logged in Successfully"
            sign_in_and_redirect User.find(authentication.user_id)
        elsif current_user
        token = omni['extra']['raw_info'].steamid
        # render :text => request.env["omniauth.auth"].info.to_hash.inspect

        puts token
        token_secret = ""

            current_user.authentications.create!(:provider => omni['provider'], 
                                                 :uid => omni['uid'], 
                                                 :token => token, 
                                                 :token_secret => token_secret)
            flash[:notice] = "Authentication successful."
            sign_in_and_redirect current_user
        else
            user = User.new
            user.apply_omniauth(omni)
        end

        if user.save
            flash[:notice] = "Logged in."
            sign_in_and_redirect User.find(user.id)             
        else
            session[:omniauth] = omni.except('extra')
            redirect_to new_user_registration_path
        end
    end




end

这部分是非常多余的,尤其是当我昨晚做了更多的修改,最终得到的蒸汽甚至工作。

它在user model中使用的函数

User.rb

代码语言:javascript
运行
复制
  def apply_omniauth(omni)
    authentications.build(:provider => omni['provider'], 
                          :uid => omni['uid'],
                          :token => omni['credentials'].token, 
                          :token_secret => omni['credentials'].secret)
  end


  def password_required?
    (authentications.empty? || !password.blank?) && super #&& provider.blank?
  end

  def self.from_omniauth(auth)
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.email = auth.info.email
        user.password = Devise.friendly_token[0,20]
      end
  end

我在为蒸汽公司填充假数据,就像我在推特上一样。

我终于让这些联接在每种形式上都起作用了,尽管最后的“否则”(不是注册,尝试新加入)并没有起作用,因为我希望在bug测试之前尽量减少冗余。

EN

回答 1

Code Review用户

发布于 2018-08-27 20:56:15

这里有一个删除冗余的选项:将特定于提供程序的defs替换为一个单独的,如下所示:

代码语言:javascript
运行
复制
def authlenticate
    # I misspelled authlenticate above on purpose
    # Steam requires a different handling of the authenticity token 
    omni = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omni['provider'], omni['uid'])

    if authentication
        flash[:notice] = "Logged in Successfully"
        sign_in_and_redirect User.find(authentication.user_id)
    elsif current_user
        token = omni['credentials'].token || omni['extra']['raw_info'].steamid
    # if steam produces a value for omni['credentials'].token then the above line will fail for steam
        token_secret = omni['credentials'].secret || ""
    # if omni['credentials'].secret is not provided (e.g. FB) then set it to ""
        current_user.authentications.create!(:provider => omni['provider'], 
                                      :uid => omni['uid'], 
                                      :token => token, 
                                      :token_secret => token_secret)
        flash[:notice] = "Authentication successful."
        sign_in_and_redirect current_user
    else
        user = User.new
        user.apply_omniauth(omni)

        if user.save
         flash[:notice] = "Logged in."
         sign_in_and_redirect User.find(user.id)             
        else
         session[:omniauth] = omni.except('extra')
         redirect_to new_user_registration_path
        end
    end 
end
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/169556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档