【问题标题】:devise token auth + Save Facebook info设计令牌身份验证 + 保存 Facebook 信息
【发布时间】:2015-10-28 06:25:19
【问题描述】:

我正在使用Devise auth token gem 和 ng-token auth 来验证我的单页应用程序。除了使用电子邮件注册外,还可以选择通过 Facebook 注册。有用。

我的问题是,对于我的 Fb 请求范围,我设置为获取 user_friends、电子邮件和 public_info,但是当我收到请求时,我没有看到 auth_hash request.env['omniauth.auth']。这在omniauth-facebook gem documentation 中有描述。

基本上,我想将 FB 回馈给我的用户的信息保存在我的数据库中。你会怎么做?

【问题讨论】:

  • github.com/plataformatec/devise/wiki/OmniAuth:-Overview 在这里您可以找到一些信息。我不知道这将如何与 Angular 一起工作,但基本上你必须实现一个回调操作,在其中你将拥有 facebook 响应哈希,然后你可以根据需要使用。
  • 谢谢@Icguida。在那个 wiki 页面上,我注意到应该在控制器操作中获取 request.env["omniauth.auth"] 。 Devise auth token gem 中的特定控制器是 DeviseTokenAuth::OmniauthCallbacksController 。并且动作是omniauth_success。但是 request.env["omniauth.auth"] 返回 nil。

标签: ruby-on-rails ruby angularjs devise


【解决方案1】:

我不知道如何使用 Angular 和 Rails 做到这一点,但这是我在 Rails 应用程序中的做法。

def show 
 #controller action
 @FbInfoforFrontEnd = FbModel.from_omniauth(request.env["omniauth.auth"])
end

在 FbModel 模型中,我会实现这样的机制来保存令牌和其余信息

def self.from_omniauth(auth)
 #FbModel model method
 oauth = Koala::Facebook::OAuth.new(AppId, AppSecret)
 new_access_info = oauth.exchange_access_token_info auth.credentials.token
 new_access_token = new_access_info["access_token"]
 new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds
 return FbModel.where(provider: auth.provider,
                      uid: auth.uid)
               .first_or_create(provider: auth.provider,
                         uid: auth.uid,
                         name: auth.info.name)
               .update_attributes(oauth_expires_at: new_access_expires_at,
                                  oauth_token: new_access_token)
end

我希望这能回答您的问题

【讨论】:

    【解决方案2】:

    我将此端点与 Ionic2 应用程序一起使用,用于 Rails 5 API 上的 Facebook 登录请求,使用 Koala 和 Devise Token Auth,因此您应该能够在任何 Rails 5 应用程序上进行复制:

    def facebook
      @graph = Koala::Facebook::API.new(oauth_params[:token], ENV["FACEBOOK_SECRET"])
      @profile = @graph.get_object("me?fields=email,first_name,last_name,picture.type(large)")
    
      unless @user = User.where(email: @profile["email"])&.first
        @user = User.new(email: @profile["email"])
      end
      #
      # Here you will make your logic for saving Facebook's data on your User model,
      # customize accordingly
      #
      if @user.new_record?
        p = SecureRandom.urlsafe_base64(nil, false)
        @user.password = p
        @user.password_confirmation = p
        #
        # "Passwordless" login, user must set password after registering,
        # or be forever haunted by indecipherable SecureRandom value
        #
        @user.name = @profile["first_name"]+ " " + @profile["last_name"]
        @user.remote_avatar_url = @profile["picture"]["data"]["url"]
        @user.confirmed_at = Time.now
        @user.uid = @profile["id"]
        @user.provider = 'Facebook'
      end
    
      @client_id = SecureRandom.urlsafe_base64(nil, false)
      @token     = SecureRandom.urlsafe_base64(nil, false)
      @user.tokens[@client_id] = {
        token: BCrypt::Password.create(@token),
        expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
      } 
      auth_header = @user.build_auth_header(@token, @client_id)
      # update the response header
      response.headers.merge!(auth_header)
      @user.save!
      if sign_in(:user, @user, store: false, bypass: false)
        render json:  {
          data: @user.token_validation_response 
        }
      end
    end
    

    将 POST 路由指向此,并将来自 Facebook 请求的 authResponse.accessToken 作为 params[:token] 传递。 瞧,身份验证魔法!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 2016-05-08
      • 2011-12-01
      相关资源
      最近更新 更多