Sinatra Warden拥有使用Devise的现有Ruby on Rails应用程序 [英] Sinatra Warden with existing Ruby on Rails application that uses Devise

查看:194
本文介绍了Sinatra Warden拥有使用Devise的现有Ruby on Rails应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拆分目前的Ruby on Rails 3 Web应用程序,它是Web服务(API)。我的Web应用程序在Heroku上运行,并在我的应用程序中实现API作为命名空间的路由。例如 / events 返回HTML页面, / api / v1 / events 返回JSON数据。

I am trying to split my current Ruby on Rails 3 web-application and it's web-services (API). My web-application is running on Heroku and implements API as a namespaced route within my application. For example /events returns a HTML page and /api/v1/events returns a JSON data.

根据最佳做法,我想将它们分成两个不同的应用程序。我选择了Sinatra来实现API应用程序。它现在可以用于不需要身份验证的简单请求。

According to some best practices, I want to split those into two different applications. I have chosen Sinatra to implement the API application. It works now for simple requests where authentication is not required.

我的Ruby on Rails 3应用程序正在使用Devise来验证用户。还可以使用Facebook帐户登录。现在我想要实现的是通过使用Warden,通过我基于Sinatra的API的用户的HTTP基本身份验证(包括注册)。

My Ruby on Rails 3 application is using Devise to authenticate users. There's also ability to login with Facebook account. Now what I want to achieve, is HTTP Basic Authentication of users (including registration) through my Sinatra-based API by using Warden.

什么是最好的方法?或者也许我可以使用与Warden不同的东西?

What is the best way to do that? Or maybe I can use something different then Warden?

请记住,我不太熟悉Rack:)

Keep in mind that I am not very familiar with Rack :)

推荐答案

我能够得到它的工作。有几个主要方面:

I was able to get it working. There were a few main aspects:


  • 让Devise使用Rails(Devise是一个Rails应用程序,不会工作
    没有

  • 在Rack级别设置映射(路由)以支持Rails和Sinatra

  • 分享Rails和Sinatra之间的会话

  • 设置监督员并将其提供给Sinatra

  • Get Devise working with Rails (Devise is a Rails app, won't work without it)
  • Setup the mapping (route) on Rack level to support both Rails and Sinatra
  • Share the sessions between Rails and Sinatra
  • Setup Warden and make it available to Sinatra

这是/config.ru中最相关的代码部分:

Here is most relevant part of code from /config.ru:

    #

    # ...

    # Rest with Rails
    map "/" do
      run MyApp::Application
    end

    # Anything urls starting with /slim will go to Sinatra
    map "/slim" do

      # make sure :key and :secret be in-sync with initializers/secret_store.rb initializers/secret_token.rb
      use Rack::Session::Cookie, :key => '<< see, initializers/secret_store.rb >>', :secret => '<< copy from initializers/secret_token.rb >>'

      # Point Warden to the Sinatra App
      use Warden::Manager do |manager|
        manager.failure_app = AppMain
        manager.default_scope = Devise.default_scope
      end

      # Borrowed from https://gist.github.com/217362
      Warden::Manager.before_failure do |env, opts|
        env['REQUEST_METHOD'] = "POST"
      end

      run AppMain
    end

请参阅 http:// labnote。 beedesk.com/sinatra-warden-rails-devise ,以获得完整的解决方案。

See, http://labnote.beedesk.com/sinatra-warden-rails-devise for a complete solution.

这篇关于Sinatra Warden拥有使用Devise的现有Ruby on Rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆