使用Ajax Omniauth授权呼叫Ajax、Omniauth

2023-09-10 19:15:20 作者:妳已经是我的独家记忆。

我有omniauth时使用它作为一个链接工作正常在我的Rails应用程序3:

I have omniauth working fine in my Rails 3 app when using it as a link:

link_to("Connect Twitter", "/auth/twitter", :id => "connect-to-twitter")

现在我想通过AJAX调用'/认证/ Twitter的。然而,没有什么是请求阶段后返回。这里是最后的日志条目:

Now I want to call '/auth/twitter' through ajax. However, nothing is returned after the request phase. Here is the last log entry:

Started GET "/auth/twitter" for 127.0.0.1 at 2012-10-30 17:53:02 -0700
(twitter) Request phase initiated.

下面是我的ajax code(在CoffeeScript中):

Here is my ajax code (in Coffeescript):

$("#connect-to-twitter").live("click", ->
  alert('get twitter')
  $.get('/auth/twitter', (data) ->
    alert(data)
  )
  return false
)

(我知道这个问题已经被问过这里,但我还没有看到任何真正的答案。)

(I know this question has been asked before here, but I haven't seen any real answer to it.)

推荐答案

我知道这个问题是有点老,但我还是会回答这个问题。面临的问题,并希望达到同样的什么OP打算我这样做之后

I know the question is bit old but I'll still answer it . After facing the issue for and looking to achieve the same what the OP intended to I did this

我写了一个中间件,并在右上方插入 Omniauth 这里怎么中间件看

I wrote a middleware and inserted in right above the Omniauth here how the middleware look

class OmniAuthMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    status, headers, body = @app.call(env)
    request = Rack::Request.new(env)
    if request.xhr? and status == 302 and request.path_info =~ /\/auth\/\w+\z/ and body.class == Rack::BodyProxy
      location = headers["Location"]
      body = ActionDispatch::Response.new
      headers = {'Content-Type'=>'text/javascript; charset=utf-8'}
      body.body = ["window.location.href='#{location}'"]
      body.headers = headers
      status = 200
    end
    [status,headers,body]
  end
end

在这里,我怎么插入由中间件在中间件​​堆栈

And here how I insert by middleware in the middleware stack

Rails.application.config.middleware.insert_before OmniAuth::Builder,OmniAuthMiddleware

下面我怎么中间件堆栈看

Here how my middleware stack look

...
...
...
use Warden::Manager
use Rack::Mongoid::Middleware::IdentityMap
use ExceptionNotifier
use OmniAuthMiddleware
use OmniAuth::Builder
run PoasterApi::Application.routes

有了这个,我能够实现我想要的东西。

With this I was able to achieve what I wanted

注意:我还没有发现有关在Omniauth doc和,因为我是对运行时间,因此我实现此修复程序(因为谷歌搜索没有给我一个满意的答复)AJAX的东西的任何信息。这可能是可能的,Omniauth支持Ajax响应很好,但因为说我是不是能够找到一个的文件中。答案是为那些谁希望实现完全相同的功能,但只是不知道如何实现它Omniauth用户。

Note: I haven't found any info about ajax stuff in the Omniauth doc and since I was running against time hence I implemented this fix (since a google search never gave me a favourable answer) . It could be possible that Omniauth support ajax response as well but as said I was not able to find one in the documentation . The answer is for those users who wish to implement the exact same functionality but just not sure how to achieve it in Omniauth .

我仍然在寻找Omniauth发现,如果他们存在的方式,在Omniauth通过设置直接做到这一点/调整配置

I'm still looking at Omniauth to find if their exist a way to do this directly in Omniauth by setting/tweaking the configuration

希望这有助于