Module: Sinatra::RbbtAuth

Defined in:
lib/rbbt/rest/common/users.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbbt/rest/common/users.rb', line 30

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

  if Rbbt.etc.web_users.exists?
    app.set :users, Rbbt.etc.web_users.yaml
  else
    app.set :users, {}
  end

  app.get '/login' do
    "<form class='login ui form' method='POST' action='/login'>" +
      "<div class='ui field'>" +
      "<label for='login_name' class='ui label'>Name: </label>" +
      "<input id='login_name' class='ui input' type='text' name='user'>" +
      "</div>" +
      "<div class='ui field'>" +
      "<label for='login_pass' class='ui label'>Pass: </label>" +
      "<input id='login_pass' class='ui input' type='password' name='pass'>" +
      "</div>" +
      "<input type='submit'>" +
    "</form>"
  end

  app.post '/login' do
    user = params[:user]
    pass = params[:pass]

    if settings.users.include?(user) and settings.users[user] == pass
      Log.warn{ "Successful login #{[user, pass] * ": "}" }
      session[:user] = user
      if session[:target_url]
        url = session.delete :target_url
        redirect to(url)
      else
        redirect to('/')
      end
    else
      Log.warn{ "Failed login attempt #{[user, pass] * ": "}" }
      session[:user] = nil
      redirect to('/login')
    end
  end
  
  app.get '/logout' do
    session[:user] = nil
    redirect to('/')
  end
end