Sinatra Warden with an existing Ruby on Rails application that uses Devise

I am trying to split my current Ruby on Rails 3 web application and its web services (APIs). My web application runs on Heroku and implements the API as a route with names in my application. For example, /events returns an HTML page, and /api/v1/events returns JSON data.

According to some best practices , I want to split them into two different applications. I chose Sinatra to implement the API application. Now it works for simple queries where authentication is not required.

My Ruby on Rails 3 uses Devise to authenticate users. There is also the ability to log in with your Facebook account. Now what I want to achieve is HTTP Basic Authentication of users (including registration) through my Sinatra-based API using Warden.

What is the best way to do this? Or maybe I can use something other than Warden?

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

+6
source share
1 answer

I was able to make it work. There were several key aspects:

  • Get Devise works with Rails (Devise application is Rails, does not work without it)
  • Configure the display (route) at the Rack level to support Rails and Sinatra
  • Share Sessions Between Rails and Sinatra
  • Clarify Warden and make it available to Sinatra

Here is the most important part of the 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 

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

+9
source

Source: https://habr.com/ru/post/886129/


All Articles