Sha256: df5a5f6c3129aaf2c22d17f4bfeb6e1206670a2dcfaad550db20b5515e2bddd2

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

require 'sinatra/base'

module Sinatra
  module SessionAuth

    module Helpers
      def authorized?
        session[:authorized]
      end

      def user
        session[:user]
      end

      def authorize!
        redirect '/login' unless authorized?
      end

      def logout!
        session[:authorized] = false
      end
    end

    def self.registered(app)
      app.helpers SessionAuth::Helpers

      app.set :users, {}

      app.get '/login' do
        "<h1>Please enter your login information</h1>" +
        "<form method='POST' action='/login'>" +
        "<label>Username</label>" +
        "<input type='text' name='user'>" +
        "<label>Password</label>" +
        "<input type='password' name='pass'>" +
        "<input type='submit' name='Submit'>" +
        "</form>"
      end

      app.post '/login' do
        Log.debug "Login: #{params.inspect}"
        if settings.users.include? params[:user] and settings.users[params[:user]] == params[:pass]
          Log.debug "Login Successful"
          session[:authorized] = true
          session[:user] = params[:user]
          session[:context] = nil
          redirect '/'
        else
          Log.debug "Login Failed"
          session[:authorized] = false
          session[:user] = nil
          session[:context] = nil
          redirect '/login'
        end
      end

      app.get '/logout' do
        session[:authorized] = false
        session[:user] = nil
        session[:context] = nil
        redirect '/'
      end

    end
  end

  register SessionAuth
end


Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rbbt-views-2.0.2 lib/rbbt/workflow/rest/auth.rb
rbbt-views-2.0.1 lib/rbbt/workflow/rest/auth.rb
rbbt-views-2.0.0 lib/rbbt/workflow/rest/auth.rb
rbbt-views-1.0.1 lib/rbbt/workflow/rest/auth.rb