I'm trying to use authentication for my Sinatra web application with Sinatra_warden module, but when I point to an authorized site, it says:
NoMethodError at /admin undefined method `authenticated?' for nil:NilClass
Here is my app.rb file:
require 'sinatra'
require 'sinatra_warden'
class App < Sinatra::Base
register Sinatra::Warden
enable :sessions
get '/' do
erb :index
end
get '/admin' do
authorize!('/login')
erb :admin
end
get '/dashboard' do
authorize!
erb :dashboard
end
end
and here is my model.rb file, just in case:
require 'rubygems'
require 'data_mapper'
require 'dm-sqlite-adapter'
require 'bcrypt'
DataMapper.setup(:default, "sqlite:test.db")
class User
include DataMapper::Resource
property :id, Serial, :key => true
property :username, String, :length => 3..50
property :password, BCryptHash
end
DataMapper.finalize
DataMapper.auto_upgrade!
What can be the problem?
The nil error is coming from the "sinatra_warden" gem, if you look into it's trying to call "request.env['warden']", which is nil because:
You have to give Sinatra the Warden Manager Rack middleware via "use"
require 'warden'
class App < Sinatra::Base
register Sinatra::Warden
enable :sessions
use Warden::Manager do |manager|
....
After you do that, you have to come up with a Warden strategy/configure the manager.
Here's an example: https://github.com/sklise/sinatra-warden-example#apprb-cont
Seems like sinatra-warden saves some boilerplate (gives you HAML login routes), but leaves it up to you to configure Warden.