From 789d21034e526a03d3e91d5d284a4888be938340 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Thu, 17 Oct 2024 00:43:31 +0200 Subject: Handle login routing at login page --- pages/login.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'pages') diff --git a/pages/login.go b/pages/login.go index fb7859a..9d8f686 100644 --- a/pages/login.go +++ b/pages/login.go @@ -11,11 +11,16 @@ import ( "net/http" "net/url" + "github.com/go-chi/chi/v5" "golang.org/x/crypto/bcrypt" ) type userContextKey struct{} +func userId(r *http.Request) int32 { + return r.Context().Value(userContextKey{}).(int32) +} + const ( sessionDuration = 86400 * 7 // 7 days loginQueryMarker = "next" @@ -46,7 +51,7 @@ func RequireAuth(next http.Handler) http.Handler { }) } -type User struct { +type user struct { Name string `form:"options=required,autofocus"` Password string `form:"type=password;options=required"` RememberMe bool `form:"type=checkbox;value=y;options=checked"` @@ -54,26 +59,24 @@ type User struct { csrf.Csrf } -func showLoginPage(w http.ResponseWriter, u User) { - showTemplate(w, "login", u) -} +func Login() Page { + r := chi.NewRouter() -func Login() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { + r.Get("/", func(w http.ResponseWriter, r *http.Request) { if session.From(r).Authenticated { http.Redirect(w, r, "/", http.StatusFound) } - u := User{} + u := user{} u.SetCsrfField(r) showLoginPage(w, u) - } -} + }) -func userId(r *http.Request) int32 { - return r.Context().Value(userContextKey{}).(int32) + r.Post("/", handleLogin) + + return r } -func checkLogin(ctx context.Context, user User) (bool, int32) { +func checkLogin(ctx context.Context, user user) (bool, int32) { dbUser, err := Q.GetUserByName(ctx, user.Name) if err == nil { hash := []byte(dbUser.Pwd) @@ -91,8 +94,8 @@ func checkLogin(ctx context.Context, user User) (bool, int32) { return true, dbUser.ID } -func HandleLogin(w http.ResponseWriter, r *http.Request) { - u := User{} +func handleLogin(w http.ResponseWriter, r *http.Request) { + u := user{} form.Parse(r, &u) ok, userId := checkLogin(r.Context(), u) @@ -121,3 +124,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) { } http.Redirect(w, r, next, http.StatusFound) } + +func showLoginPage(w http.ResponseWriter, u user) { + showTemplate(w, "login", u) +} -- cgit v1.2.3-70-g09d2 'txt' type='search' size='10' name='q' value=''/>
path: root/plugins/etc_proposals.xml (unfollow)
Commit message (Collapse)AuthorFilesLines
2007-04-18finished Qt-Frontendnecoro5-15/+117
2007-04-17marking installed packages in pkglistnecoro5-14/+50
2007-04-16added ebuild window for Qt-Frontendnecoro4-2/+229
2007-04-16added preferences for Qt-Frontendnecoro9-279/+710
2007-04-13Improved masking display and made most of the Qt-Frontend worknecoro7-35/+334
2007-04-10Some more functionality for the Qt-Frontend (complete emerge)necoro8-217/+347
2007-04-07Some more functionality for the Qt-Frontendnecoro11-88/+463
2007-04-07Added Qt-Terminalnecoro4-4/+213
2007-04-06First qt draftnecoro6-1/+796
2007-04-04showed masked packages unmasked by the user similar to stable marked testing ↵necoro5-13/+40
packages
2007-03-31changed changelognecoro1-1/+2
2007-03-31Some small changes for etcproposals 1.1necoro1-2/+2
2007-03-31Some small changes for etcproposals 1.1necoro2-3/+3
2007-03-31Some small changes for etcproposals 1.1necoro1-6/+13
2007-03-31Allowed Plugins to have a menunecoro7-201/+315
2007-03-15Added etc-proposals pluginnecoro7-28/+121
2007-03-10Added USE_EXPAND-supportnecoro5-11/+63
2007-03-10Added plugin-data to about-dialognecoro3-197/+264