Check if user already exists and bug fix

This commit is contained in:
Jason Streifling 2024-02-24 10:56:12 +01:00
parent 8ef6b6472d
commit 280e88a526
4 changed files with 17 additions and 3 deletions

View File

@ -44,7 +44,7 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool
INSERT INTO users
(username, password, first_name, last_name, writer, editor, admin)
VALUES
(?, ?, ?, ?, ?, ?)
(?, ?, ?, ?, ?, ?, ?)
`
_, err = db.Exec(query, user, string(hashedPass), first, last, writer, editor, admin)
if err != nil {

View File

@ -59,9 +59,16 @@ func HandleAddUser(db *data.DB) http.HandlerFunc {
_, _, ok := checkUserStrings(user, first, last)
if !ok {
log.Println("checkUserStrings")
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
}
id, _ := db.GetID(user)
if id != 0 {
log.Println("GetID")
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
}
if pass != pass2 {
log.Println("pass")
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
}
switch role {
@ -78,10 +85,14 @@ func HandleAddUser(db *data.DB) http.HandlerFunc {
editor = false
admin = true
default:
log.Println("switch")
template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil)
}
db.AddUser(user, pass, first, last, writer, editor, admin)
if err := db.AddUser(user, pass, first, last, writer, editor, admin); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
template.Must(template.ParseFiles("web/templates/editor.html")).Execute(w, nil)
}
}

View File

@ -30,8 +30,10 @@ func main() {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil)
})
mux.HandleFunc("POST /login/", ui.HandleLogin(db))
mux.HandleFunc("POST /add-user/", ui.HandleAddUser(db))
mux.HandleFunc("POST /finished-edit/", ui.HandleFinishedEdit(rss))
mux.HandleFunc("POST /login/", ui.HandleLogin(db))
log.Fatalln(http.ListenAndServe(":8080", mux))
}

View File

@ -1,4 +1,5 @@
{{define "page-content"}}
<h2>Neuer Benutzer</h2>
<form>
<input name="username" placeholder="Benutzername" type="text" />
<input name="password" placeholder="Passwort" type="password" />