ios - rails : How to authenticate user on mobile native with Devise+Omni-facebook+Devise_token_auth after obtaining the access token

标签 ios ruby-on-rails devise omniauth-facebook

如何在 Devise + Omniauth-facebook + Devise_token_auth 上使用 facebook 访问 token ?

有没有办法以某种方式将访问 token 挂接到 omniauth-facebook 并让用户注册/登录?或者任何其他解决方案都是受欢迎的。

我可以在我的移动端从 facebook 图形 API 获取访问 token 。

我试过将访问 token 放在回调 url 上,如下所示: /omniauth/facebook/callback?code=ACCESS_TOKEN 但它只是返回 {"errors":["使用 POST/sign_in 登录。不支持 GET。"]}

我看过:

Rails API: Authenticate users from native mobile apps using username/password or facebook token

建议看Warden的解决方案,不过我觉得没必要这么复杂。

omniauth for rails and ios authentication

建议的解决方案是 gem,但不再维护该 gem。

Rails Devise OmniAuth Facebook Login from iOS

建议的解决方案影响了移动端开发,我希望不会。

最佳答案

我决定尝试一些东西,它似乎奏效了。

我从@JeremyMoyers here 获取了代码.这确实是一个临时解决方案,但似乎可以解决问题。

我已经为 omniauth_success 覆盖了 DeviseTokenAuth Controller 操作:

路线:

config/routes.rb

namespace :api do
  scope :v1 do
    # mount_devise_token_auth_for 'User', at: 'auth'
    mount_devise_token_auth_for 'User', at: 'auth', controllers: {
      omniauth_callbacks: 'omniauth_callbacks',
      }
  end
end

Controller :

app/controllers/omniauth_callbacks_controller.rb

class OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController

  def omniauth_success
  # get_resource_from_auth_hash
  # Remove original get_resource_from_auth_hash and implement custom get_resource
  get_resource_from_uhash(params[:access_token],params[:expires_in])
  create_token_info
  set_token_on_resource
  create_auth_params

  # Skip confirmation is done at custom get_resource
  # if resource_class.devise_modules.include?(:confirmable)
  # don't send confirmation email!!!
  # @resource.skip_confirmation!
  # end

  sign_in(:user, @resource, store: false, bypass: false)

  if @resource.save!
    update_auth_header
    yield @resource if block_given?

    redirect_to @resource
    # render_data_or_redirect('deliverCredentials', @auth_params.as_json, @resource.as_json)
  else
    redirect_to rooth_path
  end
  end

protected
  def get_resource_from_uhash(access_token, expires_in)
  graph = Koala::Facebook::API.new(access_token)
  profile = graph.get_object('me?fields=email,name,id,picture')
  # logger.info(profile)
  @resource = User.where({
    uid:      profile['id'],
    provider: 'facebook'
    }).first_or_initialize do |user|
      user.provider = 'facebook'
      user.uid = profile['id']
      user.name = profile['name']
      user.oauth_token = access_token
      user.password = Devise.friendly_token[0,20]
      user.image = profile['picture'] if profile['picture'].present?
      user.oauth_expires_at = Time.at(expires_in)
      user.skip_confirmation!
      user.save!
    end

  if @resource.new_record?
    @oauth_registration = true
    set_random_password
  end
  @resource
  end
end

如果对此有任何安全问题,请告诉我。

关于ios - rails : How to authenticate user on mobile native with Devise+Omni-facebook+Devise_token_auth after obtaining the access token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37793375/

相关文章:

ios - 如何存储任意时间

ios - 如何获取屏幕的结束CGPoint?

ios - 如何从 ios 的本地库中将图像添加到 Collection View 单元格中?

ruby-on-rails - 使用 Ruby 将整数转换为行数组

ruby-on-rails - rails : How can an instance variable declared in a controller accessible in a view?

ruby-on-rails - 如何在 Rails3 的 config/initializers 下捕获所有 ActiveRecord::Errors

ruby-on-rails - rake db :migrate 的奇怪错误

ios - 更改 navigationItem.leftBarButtonItem 的位置

ruby-on-rails - 在登录之前和之后保留 Rails 4 设计 session ID

ruby-on-rails-3 - 如果从 Facebook 注销,则设计强制注销