From fb4f1754d89aaa19d627015b9af8065b88d6fb1a Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Thu, 17 Oct 2024 22:17:20 +0200 Subject: slight restructuring --- pages/login.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/pages/login.go b/pages/login.go index bd1ab46..c781f3c 100644 --- a/pages/login.go +++ b/pages/login.go @@ -16,27 +16,11 @@ import ( "golang.org/x/crypto/bcrypt" ) -type userContextKey struct{} - -func getUser(ctx context.Context) model.User { - return ctx.Value(userContextKey{}).(model.User) -} - const ( sessionDuration = 86400 * 7 // 7 days loginQueryMarker = "next" ) -func setUserInContext(ctx context.Context, uid int32) (context.Context, error) { - u, err := Q.GetUserById(ctx, uid) - if err != nil { - return ctx, err - } - - u.Pwd = "" // don't carry pwd around - return context.WithValue(ctx, userContextKey{}, u), nil -} - func RequireAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { s := session.From(r) @@ -78,7 +62,7 @@ func Login() Page { } u := user{} u.SetCsrfField(r) - showLoginPage(r, w, u) + render(login(u))(w, r) }) r.Post("/", handleLogin) @@ -116,7 +100,7 @@ func handleLogin(w http.ResponseWriter, r *http.Request) { if !ok { u.AddError("Password", "Username oder Passwort falsch.") - showLoginPage(r, w, u) + render(login(u))(w, r) return } @@ -139,6 +123,18 @@ func handleLogin(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, next, http.StatusFound) } -func showLoginPage(r *http.Request, w http.ResponseWriter, u user) { - render(login(u))(w, r) +type userContextKey struct{} + +func getUser(ctx context.Context) model.User { + return ctx.Value(userContextKey{}).(model.User) +} + +func setUserInContext(ctx context.Context, uid int32) (context.Context, error) { + u, err := Q.GetUserById(ctx, uid) + if err != nil { + return ctx, err + } + + u.Pwd = "" // don't carry pwd around + return context.WithValue(ctx, userContextKey{}, u), nil } -- cgit v1.2.3-70-g09d2