package main import ( "InfantrySkillCalculator/controllers" "InfantrySkillCalculator/utils" "github.com/gin-gonic/gin" "log" "net/http" ) func mainPage(c *gin.Context) { data := map[string]interface{}{ "isAdmin": isUserAdmin(c), } err := mainPageTemplates.Execute(c.Writer, data) if err != nil { log.Fatal(err) } } func loginPage(c *gin.Context) { session, _ := utils.Store.Get(c.Request, utils.LoginSessionName) if auth, ok := session.Values["authenticated"].(bool); ok && auth { c.Redirect(http.StatusFound, "/") return } err := loginPageTemplates.Execute(c.Writer, nil) if err != nil { log.Fatal(err) } } func loginPost(c *gin.Context) { username := c.PostForm("username") password := c.PostForm("password") if !checkUserCredentials(username, password) { c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Ungültige Logindaten!"}) return } session, _ := utils.Store.Get(c.Request, utils.LoginSessionName) session.Values["authenticated"] = true session.Values["username"] = username err := session.Save(c.Request, c.Writer) if err != nil { c.JSON(http.StatusInternalServerError, nil) return } c.Header("HX-Redirect", "/") c.String(http.StatusOK, "") } func logout(c *gin.Context) { session, _ := utils.Store.Get(c.Request, utils.LoginSessionName) session.Values["authenticated"] = false err := session.Save(c.Request, c.Writer) if err != nil { c.JSON(http.StatusInternalServerError, nil) return } c.Redirect(http.StatusFound, "/login") } func registerPage(c *gin.Context) { session, _ := utils.Store.Get(c.Request, utils.LoginSessionName) if auth, ok := session.Values["authenticated"].(bool); ok && auth { c.Redirect(http.StatusFound, "/") return } err := registerPageTemplates.Execute(c.Writer, nil) if err != nil { log.Fatal(err) } } func registerPost(c *gin.Context) { username := c.PostForm("username") password := c.PostForm("password") code := c.PostForm("code") if !isValidCode(code) { c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Ungültiger Aktivierungscode!"}) return } hashedPassword, err := hashPassword(password) if err != nil { c.HTML(http.StatusOK, "login_error.html", gin.H{"message": "Fehler beim Registrieren!"}) return } controllers.CreateUser(username, hashedPassword, true, code) session, _ := utils.Store.Get(c.Request, utils.LoginSessionName) session.Values["authenticated"] = true session.Values["username"] = username err = session.Save(c.Request, c.Writer) if err != nil { c.JSON(http.StatusInternalServerError, nil) return } c.Header("HX-Redirect", "/") c.String(http.StatusOK, "") }