diff options
Diffstat (limited to '')
-rw-r--r-- | pages/login.go (renamed from auth.go) | 81 |
1 files changed, 37 insertions, 44 deletions
@@ -1,9 +1,12 @@ -package main +package pages import ( "context" "database/sql" "errors" + "gosten/csrf" + "gosten/form" + "gosten/session" "log" "net/http" "net/url" @@ -20,9 +23,9 @@ const ( func RequireAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - s := session(r) + s := session.From(r) - if !s.s.IsNew && s.Authenticated { + if !s.IsNew() && s.Authenticated { u, err := Q.GetUserById(r.Context(), s.UserID) if err == nil { // authenticated --> done @@ -43,6 +46,33 @@ func RequireAuth(next http.Handler) http.Handler { }) } +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"` + Errors []error `form:"-"` + csrf.Csrf +} + +func showLoginPage(w http.ResponseWriter, u User) { + showTemplate(w, "login", u) +} + +func Login() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if session.From(r).Authenticated { + http.Redirect(w, r, "/", http.StatusFound) + } + u := User{} + u.SetCsrfField(r) + showLoginPage(w, u) + } +} + +func userId(r *http.Request) int32 { + return r.Context().Value(userContextKey{}).(int32) +} + func checkLogin(ctx context.Context, user User) (bool, int32) { dbUser, err := Q.GetUserByName(ctx, user.Name) if err == nil { @@ -61,19 +91,19 @@ func checkLogin(ctx context.Context, user User) (bool, int32) { return true, dbUser.ID } -func handleLogin(w http.ResponseWriter, r *http.Request) { +func HandleLogin(w http.ResponseWriter, r *http.Request) { u := User{} - parseForm(r, &u) + form.Parse(r, &u) ok, userId := checkLogin(r.Context(), u) if !ok { - u.Errors = []error{fieldError{"Password", "Invalid"}} + u.Errors = []error{form.FieldError{Field: "Password", Issue: "Invalid"}} showLoginPage(w, u) return } - s := session(r) + s := session.From(r) if u.RememberMe { s.MaxAge(sessionDuration) // 1 week } else { @@ -91,40 +121,3 @@ func handleLogin(w http.ResponseWriter, r *http.Request) { } http.Redirect(w, r, next, http.StatusFound) } - -func handleLogout() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - s := session(r) - s.Invalidate() - s.Save(w, r) - - http.Redirect(w, r, "/", http.StatusFound) - } -} - -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"` - Errors []error `form:"-"` - Csrf -} - -func showLoginPage(w http.ResponseWriter, u User) { - showTemplate(w, "login", u) -} - -func loginPage() http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - if session(r).Authenticated { - http.Redirect(w, r, "/", http.StatusFound) - } - u := User{} - u.SetCsrfField(r) - showLoginPage(w, u) - } -} - -func userId(r *http.Request) int32 { - return r.Context().Value(userContextKey{}).(int32) -} |