99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"InfantrySkillCalculator/controllers"
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"net/http"
|
|
"session"
|
|
)
|
|
|
|
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) {
|
|
if auth, ok := session.GetAuthenticated(c); 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
|
|
}
|
|
|
|
if err := session.SetLoginSession(username, c); err != nil {
|
|
c.JSON(http.StatusInternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
c.Header("HX-Redirect", "/")
|
|
c.String(http.StatusOK, "")
|
|
}
|
|
|
|
func logout(c *gin.Context) {
|
|
if err := session.InvalidateSession(c); err != nil {
|
|
c.JSON(http.StatusInternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
c.Redirect(http.StatusFound, "/login")
|
|
}
|
|
|
|
func registerPage(c *gin.Context) {
|
|
if auth, ok := session.GetAuthenticated(c); 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)
|
|
|
|
if err := session.SetLoginSession(username, c); err != nil {
|
|
c.JSON(http.StatusInternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
c.Header("HX-Redirect", "/")
|
|
c.String(http.StatusOK, "")
|
|
}
|