Module: Sinatra::SessionAuth

Defined in:
lib/rbbt/workflow/rest/auth.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rbbt/workflow/rest/auth.rb', line 24

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