From 69c0dff574cbe13bd3b9c23bde10a685dce16b2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?John=20Ankarstro=CC=88m?= <john@ankarstrom.se>
Date: Sat, 13 Mar 2021 22:05:55 +0100
Subject: Add sessions

---
 interfaces/http/admin/admin.rb | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

(limited to 'interfaces/http')

diff --git a/interfaces/http/admin/admin.rb b/interfaces/http/admin/admin.rb
index 347a051..40cbe07 100644
--- a/interfaces/http/admin/admin.rb
+++ b/interfaces/http/admin/admin.rb
@@ -1,13 +1,31 @@
 class AdminInterface < Sinatra::Base
   enable :sessions
   
+  helpers do
+    def logged_in?
+      @session != nil
+    end
+  end
+  
+  before do
+    @session = Session.where code: session[:code]
+  end
+  
   before ['/admin', '/admin/*'] do
-    redirect '/login', 303 if not false
+    redirect '/login', 303 if not logged_in?
   end
-
+  
   get '/login' do
   end
   
+  post '/login' do
+    @session = Session.start(params)
+    if @session then
+    else
+      redirect '/login', 401
+    end
+  end
+  
   get '/admin' do
     @title = $config.title
     @posts = Post.all
-- 
cgit v1.2.3