Added ability to login

This commit is contained in:
Jason Streifling 2024-02-24 09:54:25 +01:00
parent 068bf045a7
commit 2e08600814
7 changed files with 102 additions and 46 deletions

View File

@ -64,21 +64,46 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool
return nil return nil
} }
func (db *DB) ChangePassword(id int64, oldPass, newPass string) error { func (db *DB) GetID(user string) (int64, error) {
var oldHashedPass string var id int64
selectQuery := ` query := `
SELECT id FROM
users
WHERE
username = ?
`
row := db.QueryRow(query, user)
if err := row.Scan(&id); err != nil {
return 0, fmt.Errorf("user not in DB: %v", err)
}
return id, nil
}
func (db *DB) CheckPassword(id int64, pass string) error {
var queriedPass string
query := `
SELECT password FROM SELECT password FROM
users users
WHERE WHERE
id = ? id = ?
` `
row := db.QueryRow(selectQuery, id) row := db.QueryRow(query, id)
if err := row.Scan(&oldHashedPass); err != nil { if err := row.Scan(&queriedPass); err != nil {
return fmt.Errorf("error reading password from DB: %v", err) return fmt.Errorf("error reading password from DB: %v", err)
} }
if err := bcrypt.CompareHashAndPassword([]byte(oldHashedPass), []byte(oldPass)); err != nil { if err := bcrypt.CompareHashAndPassword([]byte(queriedPass), []byte(pass)); err != nil {
return fmt.Errorf("incorrect password: %v", err)
}
return nil
}
func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
if err := db.CheckPassword(id, oldPass); err != nil {
return fmt.Errorf("error checking password: %v", err) return fmt.Errorf("error checking password: %v", err)
} }
@ -88,9 +113,10 @@ func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
} }
updateQuery := ` updateQuery := `
UPDATE users UPDATE users SET
SET password = ? password = ?
WHERE id = ? WHERE
id = ?
` `
_, err = db.Exec(updateQuery, string(newHashedPass), id) _, err = db.Exec(updateQuery, string(newHashedPass), id)
if err != nil { if err != nil {

View File

@ -1,4 +1,4 @@
package articles package data
import ( import (
"bytes" "bytes"

View File

@ -1,27 +0,0 @@
package handlers
import (
"log"
"net/http"
"streifling.com/jason/cpolis/cmd/articles"
"streifling.com/jason/cpolis/cmd/feed"
)
func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := r.PostFormValue("editor-title")
desc := r.PostFormValue("editor-desc")
mdContent := r.PostFormValue("editor-text")
content, err := articles.ConvertToHTML(mdContent)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
}
feed.AddToFeed(f, title, desc, content)
feed.SaveFeed(f, "tmp/rss.gob")
// template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem)
}
}

47
cmd/ui/ui.go Normal file
View File

@ -0,0 +1,47 @@
package ui
import (
"html/template"
"log"
"net/http"
"streifling.com/jason/cpolis/cmd/data"
"streifling.com/jason/cpolis/cmd/feed"
)
func HandleLogin(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user := r.PostFormValue("username")
pass := r.PostFormValue("password")
id, err := db.GetID(user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
// TODO: und nun?
}
if err := db.CheckPassword(id, pass); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil)
}
}
}
func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := r.PostFormValue("editor-title")
desc := r.PostFormValue("editor-desc")
mdContent := r.PostFormValue("editor-text")
content, err := data.ConvertToHTML(mdContent)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Panicln(err)
}
feed.AddToFeed(f, title, desc, content)
feed.SaveFeed(f, "tmp/rss.gob")
// template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem)
}
}

11
main.go
View File

@ -7,7 +7,7 @@ import (
"streifling.com/jason/cpolis/cmd/data" "streifling.com/jason/cpolis/cmd/data"
"streifling.com/jason/cpolis/cmd/feed" "streifling.com/jason/cpolis/cmd/feed"
"streifling.com/jason/cpolis/cmd/handlers" "streifling.com/jason/cpolis/cmd/ui"
) )
func main() { func main() {
@ -17,10 +17,10 @@ func main() {
} }
defer db.Close() defer db.Close()
f, err := feed.OpenFeed("tmp/rss.gob") rss, err := feed.OpenFeed("tmp/rss.gob")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
f = feed.NewFeed("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt", rss = feed.NewFeed("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt",
"https://distrikt-ni-st.de", "https://distrikt-ni-st.de",
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität") "Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
} }
@ -28,9 +28,10 @@ func main() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/")))) mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/editor.html")).Execute(w, nil) template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil)
}) })
mux.HandleFunc("POST /finished-edit/", handlers.HandleFinishedEdit(f)) mux.HandleFunc("POST /login/", ui.HandleLogin(db))
mux.HandleFunc("POST /finished-edit/", ui.HandleFinishedEdit(rss))
log.Fatalln(http.ListenAndServe(":8080", mux)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -1,9 +1,10 @@
{{define "page-content"}} {{define "page-content"}}
<h2>Editor</h2>
<form> <form>
<input type="text" name="editor-title" value="Titel"> <input name="editor-title" placeholder="Titel" type="text" />
<textarea name="editor-desc"></textarea> <textarea name="editor-desc" placeholder="Beschreibung"></textarea>
<textarea name="editor-text"></textarea> <textarea name="editor-text" placeholder="Artikel"></textarea>
<input type="submit" value="Senden" hx-post="/finished-edit/" hx-target="#page-content"> <input type="submit" value="Senden" hx-post="/finished-edit/" hx-target="#page-content" />
</form> </form>
{{end}} {{end}}

8
web/templates/login.html Normal file
View File

@ -0,0 +1,8 @@
{{define "page-content"}}
<h2>Anmeldung</h2>
<form>
<input name="username" placeholder="Benutzername" type="text" />
<input name="password" placeholder="Passwort" type="password" />
<input type="submit" value="Anmelden" hx-post="/login/" hx-target="#page-content" />
</form>
{{end}}