flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 821746c4cd42fecad123995d2013f3c2f549df24
parent 24ee6c88b1078bc97712ad3c686c455ba152b717
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 24 Oct 2020 14:24:00 -0700

More login handling

Diffstat:
Mflounder.toml | 2+-
Mhttp.go | 35+++++++++++++++++++++++++----------
Mschema.sql | 2+-
Mtemplates/nav.html | 11+++++++----
4 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/flounder.toml b/flounder.toml @@ -9,6 +9,6 @@ CookieStoreKey="12345678123456781234567812345678" TemplatesDirectory="./templates" DBFile="./flounder.db" MaxFileSize=128000 # 128 KB -OkExtensions=[".gmi", ".txt", ".jpg", ".jpeg", ".gif", ".png", ".svg", ".webp", ".midi", ".json", ".csv", ".gemini", ".mp3", ".css", ".ttf", ".otf", ".woff", ".woff2"] +OkExtensions=["", ".gmi", ".txt", ".jpg", ".jpeg", ".gif", ".png", ".svg", ".webp", ".midi", ".json", ".csv", ".gemini", ".mp3", ".css", ".ttf", ".otf", ".woff", ".woff2"] # log file diff --git a/http.go b/http.go @@ -43,6 +43,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, fileName) return } + _, authd := getAuthUser(r) indexFiles, err := getIndexFiles() if err != nil { log.Println(err) @@ -60,7 +61,8 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { PageTitle string Files []*File Users []string - }{c.RootDomain, c.SiteTitle, indexFiles, allUsers} + LoggedIn bool + }{c.RootDomain, c.SiteTitle, indexFiles, allUsers, authd} err = t.ExecuteTemplate(w, "index.html", data) if err != nil { log.Println(err) @@ -164,10 +166,15 @@ func uploadFilesHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/my_site", 302) } -func deleteFileHandler(w http.ResponseWriter, r *http.Request) { +// bool whether auth'd, string is auth user +func getAuthUser(r *http.Request) (string, bool) { session, _ := SessionStore.Get(r, "cookie-session") - authUser, ok := session.Values["auth_user"].(string) - if !ok { + user, ok := session.Values["auth_user"].(string) + return user, ok +} +func deleteFileHandler(w http.ResponseWriter, r *http.Request) { + authUser, authd := getAuthUser(r) + if !authd { renderError(w, "Forbidden", 403) return } @@ -180,9 +187,8 @@ func deleteFileHandler(w http.ResponseWriter, r *http.Request) { } func mySiteHandler(w http.ResponseWriter, r *http.Request) { - session, _ := SessionStore.Get(r, "cookie-session") - authUser, ok := session.Values["auth_user"].(string) - if !ok { + authUser, authd := getAuthUser(r) + if !authd { renderError(w, "Forbidden", 403) return } @@ -193,7 +199,8 @@ func mySiteHandler(w http.ResponseWriter, r *http.Request) { PageTitle string AuthUser string Files []*File - }{c.RootDomain, c.SiteTitle, authUser, files} + LoggedIn bool + }{c.RootDomain, c.SiteTitle, authUser, files, authd} _ = t.ExecuteTemplate(w, "my_site.html", data) } @@ -214,9 +221,17 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { r.ParseForm() name := r.Form.Get("username") password := r.Form.Get("password") - row := DB.QueryRow("SELECT password_hash FROM user where username = $1", name) + row := DB.QueryRow("SELECT password_hash, approved FROM user where username = $1", name) var db_password []byte - _ = row.Scan(&db_password) + var active bool + _ = row.Scan(&db_password, &active) + if !active { + data := struct { + Error string + PageTitle string + }{"Your account is not active yet. Pending admin approval", c.SiteTitle} + t.ExecuteTemplate(w, "login.html", data) + } if bcrypt.CompareHashAndPassword(db_password, []byte(password)) == nil { log.Println("logged in") session, _ := SessionStore.Get(r, "cookie-session") diff --git a/schema.sql b/schema.sql @@ -3,7 +3,7 @@ CREATE TABLE user ( username TEXT NOT NULL UNIQUE, email TEXT NOT NULL UNIQUE, password_hash TEXT NOT NULL, - approved boolean NOT NULL DEFAULT false, + active boolean NOT NULL DEFAULT false, created_at INTEGER DEFAULT (strftime('%s', 'now')) ); diff --git a/templates/nav.html b/templates/nav.html @@ -1,7 +1,10 @@ <nav> <a href="/">home</a> - <a href="/my_site">/my_site</a> - <a href="/register">/register</a> - <a href="/logout">/logout</a> - <a href="/login">/login</a> +{{ if .LoggedIn }} + <a href="/my_site">my_site</a> + <a href="/logout">logout</a> +{{ else }} + <a href="/register">register</a> + <a href="/login">login</a> +{{ end }} </nav>