login

<     >

2023-08-16 15:06:40 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

add "after" query parameter to login

diff --git a/cogit/cogit.lua b/cogit/cogit.lua
index 618f53f..1f0a3f7 100644
--- a/cogit/cogit.lua
+++ b/cogit/cogit.lua
@@ -136,7 +136,8 @@ function Cogit:routes()
         if is_admin then  -- already logged in
             return "/", 303
         else
-            return {fname="view/login.html", env={title=self.title}}
+            local env = {title=self.title, after=req.query.after}
+            return {fname="view/login.html", env=env}
         end
     end},
     {"POST", "/login",
@@ -149,8 +150,9 @@ function Cogit:routes()
             local session_id = lud.crypt.b64_enc(lud.crypt.uuid4())
             self.sessions[session_id] = true
             self:log(LOG_INFO, "logged in")
+            local path = req.query.after or "/"
             local cookie = {key="sid", val=session_id, path="/", age=self.session_age}
-            return "/", 303, "See Other", {cookie}
+            return path, 303, "See Other", {cookie}
         else
             self:log(LOG_WARN, "invalid password")
         end
@@ -173,7 +175,7 @@ function Cogit:routes()
         end
         local is_admin = self:is_admin(req.cookies)
         if not is_admin and is_private(self.descs[rname]) then
-            return "/login", 303
+            return "/login?after="..req.path, 303
         end
         local bnames = repo:branches()
         local tnames = repo:tags()
@@ -191,7 +193,7 @@ function Cogit:routes()
         end
         local is_admin = self:is_admin(req.cookies)
         if not is_admin and is_private(self.descs[rname]) then
-            return "/login", 303
+            return "/login?after="..req.path, 303
         end
         local commit = repo:commit(first)
         if commit == nil then
@@ -212,7 +214,7 @@ function Cogit:routes()
         end
         local is_admin = self:is_admin(req.cookies)
         if not is_admin and is_private(self.descs[rname]) then
-            return "/login", 303
+            return "/login?after="..req.path, 303
         end
         local commit = repo:commit(cid)
         if commit == nil then
@@ -236,7 +238,7 @@ function Cogit:routes()
         end
         local is_admin = self:is_admin(req.cookies)
         if not is_admin and is_private(self.descs[rname]) then
-            return "/login", 303
+            return "/login?after="..req.path, 303
         end
         local commit = repo:commit(cid)
         if commit == nil then
@@ -263,7 +265,7 @@ function Cogit:routes()
         end
         local is_admin = self:is_admin(req.cookies)
         if not is_admin and is_private(self.descs[rname]) then
-            return "/login", 303
+            return "/login?after="..req.path, 303
         end
         local commit = repo:commit(cid)
         if commit == nil then

diff --git a/view/login.html b/view/login.html
index e17ae9b..773652f 100644
--- a/view/login.html
+++ b/view/login.html
@@ -44,7 +44,12 @@
 </head>
 <body>
   <h1 class="centered">Login</h1>
-  <form action="/login" method="post">
+  % if $after ~= nil then
+    % set query = "?after=" .. $after
+  % else
+    % set query = ""
+  % end
+  <form action="/login{{$query}}" method="post">
     <ul class="centered ul-form">
       <li><input type="password" class="flat-field" name="password" placeholder="Password" autofocus></li>
       <li><input type="submit" class="flat-button" value="Login"></li>