cerrado @ f5c307668c7fa3c367defc6e924d7c1e00303c16

feat: Add login redirect

Now login will return to page before the user clicked on login
diff --git a/pkg/handler/auth/login.go b/pkg/handler/auth/login.go
index 7014548c0965def2eba242a03ae269b8c27f7a00..89fd87b21cb316c68f253ff21a7dcc38a8359fdf 100644
--- a/pkg/handler/auth/login.go
+++ b/pkg/handler/auth/login.go
@@ -45,10 +45,19 @@ 	return nil
 }
 
 func (g *LoginHandler) Login(w http.ResponseWriter, r *http.Request) error {
+	referer := r.URL.Query().Get("referer")
+
+	// if query value is empty tries to get from header
+	if referer == "" {
+		referer = r.Header.Get("Referer")
+	}
+
 	if r.Method == "GET" {
 		ext.SetHTML(w)
 
-		login := &templates.LoginPage{}
+		login := &templates.LoginPage{
+			Referer: referer,
+		}
 		templates.WritePageTemplate(w, login, r.Context())
 	} else if r.Method == "POST" {
 
@@ -57,6 +66,7 @@ 		password := r.FormValue("password")
 
 		if !g.auth.CheckAuth(username, password) {
 			login := &templates.LoginPage{
+				Referer:      referer,
 				ErrorMessage: "Invalid login",
 			}
 			templates.WritePageTemplate(w, login, r.Context())
@@ -78,7 +88,7 @@ 				SameSite: http.SameSiteStrictMode,
 			}
 
 			http.SetCookie(w, cookie)
-			ext.Redirect(w, "/")
+			ext.Redirect(w, referer)
 		}
 
 	}
diff --git a/templates/login.qtpl b/templates/login.qtpl
index 7815bd7401ff8cdee199144ac88b61421f253eb7..efaf329ded970e0824ee1fc75f52df9be2987e34 100644
--- a/templates/login.qtpl
+++ b/templates/login.qtpl
@@ -2,6 +2,7 @@ {% import "context" %}
 
 {% code
 type LoginPage struct {
+    Referer string
     ErrorMessage string
 }
 %}
@@ -13,7 +14,11 @@
 {% func (p *LoginPage) Content(ctx context.Context) %}
 <div class="row">
   <div class="col-md-6 offset-md-3">
+  {% if p.Referer == "" %}
     <form action="/login/" method="POST">
+  {% else %}
+    <form action="/login/?referer={%s p.Referer %}" method="POST">
+  {% endif %}
       <div class="form-group m-3">
         <label for="username" class="form-label">Username</label>
         <input type="text" class="form-control" name="username" id="username">
diff --git a/templates/login.qtpl.go b/templates/login.qtpl.go
index 5c07a44eea42027c0c04ab1e56ec192c187964cb..1a1b6d7df883c28aa437cdb65d26e0bc17499229 100644
--- a/templates/login.qtpl.go
+++ b/templates/login.qtpl.go
@@ -22,82 +22,103 @@ )
 
 //line templates/login.qtpl:4
 type LoginPage struct {
+	Referer      string
 	ErrorMessage string
 }
 
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 func (p *LoginPage) StreamTitle(qw422016 *qt422016.Writer, ctx context.Context) {
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	qw422016.N().S(`Hello`)
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 }
 
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 func (p *LoginPage) WriteTitle(qq422016 qtio422016.Writer, ctx context.Context) {
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	qw422016 := qt422016.AcquireWriter(qq422016)
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	p.StreamTitle(qw422016, ctx)
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	qt422016.ReleaseWriter(qw422016)
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 }
 
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 func (p *LoginPage) Title(ctx context.Context) string {
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	qb422016 := qt422016.AcquireByteBuffer()
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	p.WriteTitle(qb422016, ctx)
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	qs422016 := string(qb422016.B)
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	qt422016.ReleaseByteBuffer(qb422016)
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 	return qs422016
-//line templates/login.qtpl:9
+//line templates/login.qtpl:10
 }
 
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 func (p *LoginPage) StreamNavbar(qw422016 *qt422016.Writer, ctx context.Context) {
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	StreamNavbar(qw422016, ctx, Login)
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 }
 
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 func (p *LoginPage) WriteNavbar(qq422016 qtio422016.Writer, ctx context.Context) {
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	qw422016 := qt422016.AcquireWriter(qq422016)
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	p.StreamNavbar(qw422016, ctx)
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	qt422016.ReleaseWriter(qw422016)
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 }
 
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 func (p *LoginPage) Navbar(ctx context.Context) string {
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	qb422016 := qt422016.AcquireByteBuffer()
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	p.WriteNavbar(qb422016, ctx)
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	qs422016 := string(qb422016.B)
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	qt422016.ReleaseByteBuffer(qb422016)
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 	return qs422016
-//line templates/login.qtpl:11
+//line templates/login.qtpl:12
 }
 
-//line templates/login.qtpl:13
+//line templates/login.qtpl:14
 func (p *LoginPage) StreamContent(qw422016 *qt422016.Writer, ctx context.Context) {
-//line templates/login.qtpl:13
+//line templates/login.qtpl:14
 	qw422016.N().S(`
 <div class="row">
   <div class="col-md-6 offset-md-3">
+  `)
+//line templates/login.qtpl:17
+	if p.Referer == "" {
+//line templates/login.qtpl:17
+		qw422016.N().S(`
     <form action="/login/" method="POST">
+  `)
+//line templates/login.qtpl:19
+	} else {
+//line templates/login.qtpl:19
+		qw422016.N().S(`
+    <form action="/login/?referer=`)
+//line templates/login.qtpl:20
+		qw422016.E().S(p.Referer)
+//line templates/login.qtpl:20
+		qw422016.N().S(`" method="POST">
+  `)
+//line templates/login.qtpl:21
+	}
+//line templates/login.qtpl:21
+	qw422016.N().S(`
       <div class="form-group m-3">
         <label for="username" class="form-label">Username</label>
         <input type="text" class="form-control" name="username" id="username">
@@ -112,85 +133,85 @@       </div>
     </form>
   </div>
   `)
-//line templates/login.qtpl:30
+//line templates/login.qtpl:35
 	if p.ErrorMessage != "" {
-//line templates/login.qtpl:30
+//line templates/login.qtpl:35
 		qw422016.N().S(`
   <div class="col-md-6 offset-md-3">
     <div class="alert alert-warning text-center" >
         `)
-//line templates/login.qtpl:33
+//line templates/login.qtpl:38
 		qw422016.E().S(p.ErrorMessage)
-//line templates/login.qtpl:33
+//line templates/login.qtpl:38
 		qw422016.N().S(`
     </div>
   </div>
   `)
-//line templates/login.qtpl:36
+//line templates/login.qtpl:41
 	}
-//line templates/login.qtpl:36
+//line templates/login.qtpl:41
 	qw422016.N().S(`
 </div>
 `)
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 }
 
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 func (p *LoginPage) WriteContent(qq422016 qtio422016.Writer, ctx context.Context) {
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 	qw422016 := qt422016.AcquireWriter(qq422016)
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 	p.StreamContent(qw422016, ctx)
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 	qt422016.ReleaseWriter(qw422016)
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 }
 
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 func (p *LoginPage) Content(ctx context.Context) string {
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 	qb422016 := qt422016.AcquireByteBuffer()
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 	p.WriteContent(qb422016, ctx)
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 	qs422016 := string(qb422016.B)
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 	qt422016.ReleaseByteBuffer(qb422016)
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 	return qs422016
-//line templates/login.qtpl:38
+//line templates/login.qtpl:43
 }
 
-//line templates/login.qtpl:40
+//line templates/login.qtpl:45
 func (p *LoginPage) StreamScript(qw422016 *qt422016.Writer, ctx context.Context) {
-//line templates/login.qtpl:40
+//line templates/login.qtpl:45
 	qw422016.N().S(`
 `)
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 }
 
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 func (p *LoginPage) WriteScript(qq422016 qtio422016.Writer, ctx context.Context) {
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 	qw422016 := qt422016.AcquireWriter(qq422016)
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 	p.StreamScript(qw422016, ctx)
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 	qt422016.ReleaseWriter(qw422016)
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 }
 
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 func (p *LoginPage) Script(ctx context.Context) string {
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 	qb422016 := qt422016.AcquireByteBuffer()
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 	p.WriteScript(qb422016, ctx)
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 	qs422016 := string(qb422016.B)
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 	qt422016.ReleaseByteBuffer(qb422016)
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 	return qs422016
-//line templates/login.qtpl:41
+//line templates/login.qtpl:46
 }