Skip to content

Instantly share code, notes, and snippets.

@thomasyip
Created October 31, 2011 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomasyip/1326830 to your computer and use it in GitHub Desktop.
Save thomasyip/1326830 to your computer and use it in GitHub Desktop.
Sinatra + Warden & Rails + Devise Example
# /config.ru
# This file is used by Rack-based servers to start the application.
# File generated by "rails create MyApp"
# For Rails
require ::File.expand_path('../config/environment', __FILE__)
# For Sinatra
require './slim/slim.rb'
# - Make sinatra play nice
use Rack::MethodOverride
disable :run, :reload
# Mapping
# -------
# 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
# /slim/slim.rb
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require 'rubygems'
require 'sinatra'
require 'warden'
require 'json'
# =============
# Const
# =============
RACK_ENV = "#{ENV['RACK_ENV']}".downcase
EMPTY_JSON = JSON.generate({})
class AppMain < Sinatra::Application
set :root, APP_ROOT
get '/' do
redirect '/app.html'
end
get '/protected' do
if env['warden'].unauthenticated?
halt 401, "User is not logged in."
end
'Hello World'
end
# -------------
# Auth
# -------------
# See, from https://gist.github.com/217362
post '/unauthenticated/?' do
options = env['warden.options'] || {}
code = options[:code] || 401
message = options[:message] || 'Unauthorized'
json = {
:code => code,
:message => message,
:options => options
}
halt code, JSON.generate(json)
end
get '/logout/?' do
env['warden'].logout
redirect '/app.html'
end
end
@thomasyip
Copy link
Author

Along the way, you might have seen some of these errors. Hopefully, by listing them, Google will direct a few of you here and save you some hairs:

  • no marshal_dump is defined for class Proc
  • rack-1.0.1/lib/rack/session/cookie.rb:64:in 'dump'
  • undefined method `unauthenticated?' for nil:NilClass
  • if env['warden'].unauthenticated?
  • file: whiny_nil.rb location: method_missing line: 48
  • manager.default_scope = Devise.default_scope

@thomasyip
Copy link
Author

@thomasyip
Copy link
Author

One missing step was to update this file:
/config/initializers/session_store.rb

Patch:
diff --git a/config/initializers/session_store.rb b/config/initializers/session_store.rb
index 175240b..446d459 100755
--- a/config/initializers/session_store.rb
+++ b/config/initializers/session_store.rb
@@ -1,6 +1,6 @@

Be sure to restart your server when you modify this file.

-MyApp::Application.config.session_store :cookie_store, :key => '_MyApp_session'
+MyApp::Application.config.session_store :cookie_store, :key => '_common_session_id'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment