cerrado @ f5c307668c7fa3c367defc6e924d7c1e00303c16

feat: Add login redirect

Now login will return to page before the user clicked on login
  1diff --git a/pkg/handler/auth/login.go b/pkg/handler/auth/login.go
  2index 7014548c0965def2eba242a03ae269b8c27f7a00..89fd87b21cb316c68f253ff21a7dcc38a8359fdf 100644
  3--- a/pkg/handler/auth/login.go
  4+++ b/pkg/handler/auth/login.go
  5@@ -45,10 +45,19 @@ 	return nil
  6 }
  7 
  8 func (g *LoginHandler) Login(w http.ResponseWriter, r *http.Request) error {
  9+	referer := r.URL.Query().Get("referer")
 10+
 11+	// if query value is empty tries to get from header
 12+	if referer == "" {
 13+		referer = r.Header.Get("Referer")
 14+	}
 15+
 16 	if r.Method == "GET" {
 17 		ext.SetHTML(w)
 18 
 19-		login := &templates.LoginPage{}
 20+		login := &templates.LoginPage{
 21+			Referer: referer,
 22+		}
 23 		templates.WritePageTemplate(w, login, r.Context())
 24 	} else if r.Method == "POST" {
 25 
 26@@ -57,6 +66,7 @@ 		password := r.FormValue("password")
 27 
 28 		if !g.auth.CheckAuth(username, password) {
 29 			login := &templates.LoginPage{
 30+				Referer:      referer,
 31 				ErrorMessage: "Invalid login",
 32 			}
 33 			templates.WritePageTemplate(w, login, r.Context())
 34@@ -78,7 +88,7 @@ 				SameSite: http.SameSiteStrictMode,
 35 			}
 36 
 37 			http.SetCookie(w, cookie)
 38-			ext.Redirect(w, "/")
 39+			ext.Redirect(w, referer)
 40 		}
 41 
 42 	}
 43diff --git a/templates/login.qtpl b/templates/login.qtpl
 44index 7815bd7401ff8cdee199144ac88b61421f253eb7..efaf329ded970e0824ee1fc75f52df9be2987e34 100644
 45--- a/templates/login.qtpl
 46+++ b/templates/login.qtpl
 47@@ -2,6 +2,7 @@ {% import "context" %}
 48 
 49 {% code
 50 type LoginPage struct {
 51+    Referer string
 52     ErrorMessage string
 53 }
 54 %}
 55@@ -13,7 +14,11 @@
 56 {% func (p *LoginPage) Content(ctx context.Context) %}
 57 <div class="row">
 58   <div class="col-md-6 offset-md-3">
 59+  {% if p.Referer == "" %}
 60     <form action="/login/" method="POST">
 61+  {% else %}
 62+    <form action="/login/?referer={%s p.Referer %}" method="POST">
 63+  {% endif %}
 64       <div class="form-group m-3">
 65         <label for="username" class="form-label">Username</label>
 66         <input type="text" class="form-control" name="username" id="username">
 67diff --git a/templates/login.qtpl.go b/templates/login.qtpl.go
 68index 5c07a44eea42027c0c04ab1e56ec192c187964cb..1a1b6d7df883c28aa437cdb65d26e0bc17499229 100644
 69--- a/templates/login.qtpl.go
 70+++ b/templates/login.qtpl.go
 71@@ -22,82 +22,103 @@ )
 72 
 73 //line templates/login.qtpl:4
 74 type LoginPage struct {
 75+	Referer      string
 76 	ErrorMessage string
 77 }
 78 
 79-//line templates/login.qtpl:9
 80+//line templates/login.qtpl:10
 81 func (p *LoginPage) StreamTitle(qw422016 *qt422016.Writer, ctx context.Context) {
 82-//line templates/login.qtpl:9
 83+//line templates/login.qtpl:10
 84 	qw422016.N().S(`Hello`)
 85-//line templates/login.qtpl:9
 86+//line templates/login.qtpl:10
 87 }
 88 
 89-//line templates/login.qtpl:9
 90+//line templates/login.qtpl:10
 91 func (p *LoginPage) WriteTitle(qq422016 qtio422016.Writer, ctx context.Context) {
 92-//line templates/login.qtpl:9
 93+//line templates/login.qtpl:10
 94 	qw422016 := qt422016.AcquireWriter(qq422016)
 95-//line templates/login.qtpl:9
 96+//line templates/login.qtpl:10
 97 	p.StreamTitle(qw422016, ctx)
 98-//line templates/login.qtpl:9
 99+//line templates/login.qtpl:10
100 	qt422016.ReleaseWriter(qw422016)
101-//line templates/login.qtpl:9
102+//line templates/login.qtpl:10
103 }
104 
105-//line templates/login.qtpl:9
106+//line templates/login.qtpl:10
107 func (p *LoginPage) Title(ctx context.Context) string {
108-//line templates/login.qtpl:9
109+//line templates/login.qtpl:10
110 	qb422016 := qt422016.AcquireByteBuffer()
111-//line templates/login.qtpl:9
112+//line templates/login.qtpl:10
113 	p.WriteTitle(qb422016, ctx)
114-//line templates/login.qtpl:9
115+//line templates/login.qtpl:10
116 	qs422016 := string(qb422016.B)
117-//line templates/login.qtpl:9
118+//line templates/login.qtpl:10
119 	qt422016.ReleaseByteBuffer(qb422016)
120-//line templates/login.qtpl:9
121+//line templates/login.qtpl:10
122 	return qs422016
123-//line templates/login.qtpl:9
124+//line templates/login.qtpl:10
125 }
126 
127-//line templates/login.qtpl:11
128+//line templates/login.qtpl:12
129 func (p *LoginPage) StreamNavbar(qw422016 *qt422016.Writer, ctx context.Context) {
130-//line templates/login.qtpl:11
131+//line templates/login.qtpl:12
132 	StreamNavbar(qw422016, ctx, Login)
133-//line templates/login.qtpl:11
134+//line templates/login.qtpl:12
135 }
136 
137-//line templates/login.qtpl:11
138+//line templates/login.qtpl:12
139 func (p *LoginPage) WriteNavbar(qq422016 qtio422016.Writer, ctx context.Context) {
140-//line templates/login.qtpl:11
141+//line templates/login.qtpl:12
142 	qw422016 := qt422016.AcquireWriter(qq422016)
143-//line templates/login.qtpl:11
144+//line templates/login.qtpl:12
145 	p.StreamNavbar(qw422016, ctx)
146-//line templates/login.qtpl:11
147+//line templates/login.qtpl:12
148 	qt422016.ReleaseWriter(qw422016)
149-//line templates/login.qtpl:11
150+//line templates/login.qtpl:12
151 }
152 
153-//line templates/login.qtpl:11
154+//line templates/login.qtpl:12
155 func (p *LoginPage) Navbar(ctx context.Context) string {
156-//line templates/login.qtpl:11
157+//line templates/login.qtpl:12
158 	qb422016 := qt422016.AcquireByteBuffer()
159-//line templates/login.qtpl:11
160+//line templates/login.qtpl:12
161 	p.WriteNavbar(qb422016, ctx)
162-//line templates/login.qtpl:11
163+//line templates/login.qtpl:12
164 	qs422016 := string(qb422016.B)
165-//line templates/login.qtpl:11
166+//line templates/login.qtpl:12
167 	qt422016.ReleaseByteBuffer(qb422016)
168-//line templates/login.qtpl:11
169+//line templates/login.qtpl:12
170 	return qs422016
171-//line templates/login.qtpl:11
172+//line templates/login.qtpl:12
173 }
174 
175-//line templates/login.qtpl:13
176+//line templates/login.qtpl:14
177 func (p *LoginPage) StreamContent(qw422016 *qt422016.Writer, ctx context.Context) {
178-//line templates/login.qtpl:13
179+//line templates/login.qtpl:14
180 	qw422016.N().S(`
181 <div class="row">
182   <div class="col-md-6 offset-md-3">
183+  `)
184+//line templates/login.qtpl:17
185+	if p.Referer == "" {
186+//line templates/login.qtpl:17
187+		qw422016.N().S(`
188     <form action="/login/" method="POST">
189+  `)
190+//line templates/login.qtpl:19
191+	} else {
192+//line templates/login.qtpl:19
193+		qw422016.N().S(`
194+    <form action="/login/?referer=`)
195+//line templates/login.qtpl:20
196+		qw422016.E().S(p.Referer)
197+//line templates/login.qtpl:20
198+		qw422016.N().S(`" method="POST">
199+  `)
200+//line templates/login.qtpl:21
201+	}
202+//line templates/login.qtpl:21
203+	qw422016.N().S(`
204       <div class="form-group m-3">
205         <label for="username" class="form-label">Username</label>
206         <input type="text" class="form-control" name="username" id="username">
207@@ -112,85 +133,85 @@       </div>
208     </form>
209   </div>
210   `)
211-//line templates/login.qtpl:30
212+//line templates/login.qtpl:35
213 	if p.ErrorMessage != "" {
214-//line templates/login.qtpl:30
215+//line templates/login.qtpl:35
216 		qw422016.N().S(`
217   <div class="col-md-6 offset-md-3">
218     <div class="alert alert-warning text-center" >
219         `)
220-//line templates/login.qtpl:33
221+//line templates/login.qtpl:38
222 		qw422016.E().S(p.ErrorMessage)
223-//line templates/login.qtpl:33
224+//line templates/login.qtpl:38
225 		qw422016.N().S(`
226     </div>
227   </div>
228   `)
229-//line templates/login.qtpl:36
230+//line templates/login.qtpl:41
231 	}
232-//line templates/login.qtpl:36
233+//line templates/login.qtpl:41
234 	qw422016.N().S(`
235 </div>
236 `)
237-//line templates/login.qtpl:38
238+//line templates/login.qtpl:43
239 }
240 
241-//line templates/login.qtpl:38
242+//line templates/login.qtpl:43
243 func (p *LoginPage) WriteContent(qq422016 qtio422016.Writer, ctx context.Context) {
244-//line templates/login.qtpl:38
245+//line templates/login.qtpl:43
246 	qw422016 := qt422016.AcquireWriter(qq422016)
247-//line templates/login.qtpl:38
248+//line templates/login.qtpl:43
249 	p.StreamContent(qw422016, ctx)
250-//line templates/login.qtpl:38
251+//line templates/login.qtpl:43
252 	qt422016.ReleaseWriter(qw422016)
253-//line templates/login.qtpl:38
254+//line templates/login.qtpl:43
255 }
256 
257-//line templates/login.qtpl:38
258+//line templates/login.qtpl:43
259 func (p *LoginPage) Content(ctx context.Context) string {
260-//line templates/login.qtpl:38
261+//line templates/login.qtpl:43
262 	qb422016 := qt422016.AcquireByteBuffer()
263-//line templates/login.qtpl:38
264+//line templates/login.qtpl:43
265 	p.WriteContent(qb422016, ctx)
266-//line templates/login.qtpl:38
267+//line templates/login.qtpl:43
268 	qs422016 := string(qb422016.B)
269-//line templates/login.qtpl:38
270+//line templates/login.qtpl:43
271 	qt422016.ReleaseByteBuffer(qb422016)
272-//line templates/login.qtpl:38
273+//line templates/login.qtpl:43
274 	return qs422016
275-//line templates/login.qtpl:38
276+//line templates/login.qtpl:43
277 }
278 
279-//line templates/login.qtpl:40
280+//line templates/login.qtpl:45
281 func (p *LoginPage) StreamScript(qw422016 *qt422016.Writer, ctx context.Context) {
282-//line templates/login.qtpl:40
283+//line templates/login.qtpl:45
284 	qw422016.N().S(`
285 `)
286-//line templates/login.qtpl:41
287+//line templates/login.qtpl:46
288 }
289 
290-//line templates/login.qtpl:41
291+//line templates/login.qtpl:46
292 func (p *LoginPage) WriteScript(qq422016 qtio422016.Writer, ctx context.Context) {
293-//line templates/login.qtpl:41
294+//line templates/login.qtpl:46
295 	qw422016 := qt422016.AcquireWriter(qq422016)
296-//line templates/login.qtpl:41
297+//line templates/login.qtpl:46
298 	p.StreamScript(qw422016, ctx)
299-//line templates/login.qtpl:41
300+//line templates/login.qtpl:46
301 	qt422016.ReleaseWriter(qw422016)
302-//line templates/login.qtpl:41
303+//line templates/login.qtpl:46
304 }
305 
306-//line templates/login.qtpl:41
307+//line templates/login.qtpl:46
308 func (p *LoginPage) Script(ctx context.Context) string {
309-//line templates/login.qtpl:41
310+//line templates/login.qtpl:46
311 	qb422016 := qt422016.AcquireByteBuffer()
312-//line templates/login.qtpl:41
313+//line templates/login.qtpl:46
314 	p.WriteScript(qb422016, ctx)
315-//line templates/login.qtpl:41
316+//line templates/login.qtpl:46
317 	qs422016 := string(qb422016.B)
318-//line templates/login.qtpl:41
319+//line templates/login.qtpl:46
320 	qt422016.ReleaseByteBuffer(qb422016)
321-//line templates/login.qtpl:41
322+//line templates/login.qtpl:46
323 	return qs422016
324-//line templates/login.qtpl:41
325+//line templates/login.qtpl:46
326 }