// Function to serve the login page func getLoginHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) { if database.RetrieveUsersCount() == 0 { http.Redirect(w, r, "/admin/register/", 302) return } http.ServeFile(w, r, filepath.Join(filenames.AdminFilepath, "login.html")) return }
// Function to route the /admin/ url accordingly. (Is user logged in? Is at least one user registered?) func adminHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) { if database.RetrieveUsersCount() == 0 { http.Redirect(w, r, "/admin/register/", 302) return } else { userName := authentication.GetUserName(r) if userName != "" { http.ServeFile(w, r, filepath.Join(filenames.AdminFilepath, "admin.html")) return } else { http.Redirect(w, r, "/admin/login/", 302) return } } }
// Function to recieve a registration form. func postRegistrationHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) { if database.RetrieveUsersCount() == 0 { // TODO: Or check if authenticated user is admin when adding users from inside the admin area name := r.FormValue("name") email := r.FormValue("email") password := r.FormValue("password") if name != "" && password != "" { hashedPassword, err := authentication.EncryptPassword(password) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } user := structure.User{Name: []byte(name), Slug: slug.Generate(name, "users"), Email: []byte(email), Image: []byte(filenames.DefaultUserImageFilename), Cover: []byte(filenames.DefaultUserCoverFilename), Role: 4} err = methods.SaveUser(&user, hashedPassword, 1) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } http.Redirect(w, r, "/admin/", 302) return } http.Redirect(w, r, "/admin/", 302) return } else { name := r.FormValue("name") email := r.FormValue("email") password := r.FormValue("password") if name != "" && password != "" { hashedPassword, err := authentication.EncryptPassword(password) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } user := structure.User{Name: []byte(name), Slug: slug.Generate(name, "users"), Email: []byte(email), Image: []byte(filenames.DefaultUserImageFilename), Cover: []byte(filenames.DefaultUserCoverFilename), Role: 3} err = methods.SaveUser(&user, hashedPassword, 1) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } http.Redirect(w, r, "/admin/#/users/", 302) return } else { http.Error(w, "Not implemented yet.", http.StatusInternalServerError) return } } }