Devise with Sinatra
Devise is really just a Rails-centric wrapper with nice helpers for warden, which is the underlying Rack authentication framework. So if you're using Sinatra in conjunction with Rails, you can use Devise in your Rails app, and use warden directly in your Sinatra app, and they will see the same user session data.
So no, you can't use Devise directly within your Sinatra app, but if you're building a modular app with some pieces in Rails, and other pieces in Sinatra, you can use Devise/Warden among the components.
Sinatra Warden with existing Ruby on Rails application that uses Devise
I was able to get it working. There were a few main aspects:
- 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
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
See, http://labnote.beedesk.com/sinatra-warden-rails-devise for a complete solution.
Related Topics
Ruby on Rails User Registration Using Rest API Call and Devise
Rails 4 Update Nested Attributes
Checking If a String Is Valid JSON Before Trying to Parse It
Ruby - How to Write a New File with Output from Script
Wicked-Pdf Not Showing Images, 'Wicked_Pdf_Image_Tag' Undefined
Ruby Array to Hash: Each Element the Key and Derive Value from It
Override the Protect_From_Forgery Strategy in a Controller
How to Disable a Form Submit Button "A Là Ruby on Rails Way"
Ruby Operator Overloading Question
How to Push Keys and Values into an Empty Hash W/ Ruby
What Grammar Based Parser-Generator Tools Exist for Ruby
Rails: Update Model Attribute Without Invoking Callbacks
Carrierwave Fog Amazon S3 Images Not Displaying
Differencebetween 'File.Read' and 'Io.Read'
In Ruby What's the Difference Between Self.Method and a Method Within Class << Self