Created
August 26, 2013 01:57
-
-
Save acook/6337512 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AuthEngine < Sinatra::Base | |
set :sessions => true | |
register do | |
def auth type | |
condition do | |
redirect "/login" unless send "is_#{type}?" | |
end | |
end | |
end | |
helpers do | |
def is_user? | |
!!@user | |
end | |
end | |
before do | |
@user = User.find session[:user_id] | |
end | |
post '/login' do | |
user = User.authenticate params | |
if user then | |
session[:user_id] = user.id | |
else | |
redirect to('/login') | |
end | |
end | |
get '/logout' do | |
session[:user_id] = nil | |
redirect '/' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment