97 lines
2.2 KiB
Go
97 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"InfrantrySkillCalculator/models"
|
|
"github.com/gin-gonic/gin"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func mainPage(c *gin.Context) {
|
|
files := []string{
|
|
"./templates/index.html",
|
|
"./templates/components/home_clan_bar.html",
|
|
"./templates/components/opp_clan_bar.html",
|
|
"./templates/components/home_player_list.html",
|
|
"./templates/components/opp_player_list.html",
|
|
"./templates/components/bottom_controls.html",
|
|
"./templates/modals/delete_clan.html",
|
|
"./templates/modals/add_clan.html",
|
|
"./templates/modals/edit_clan.html",
|
|
"./templates/modals/add_player.html",
|
|
"./templates/modals/delete_player.html",
|
|
"./templates/modals/edit_player.html",
|
|
"./templates/components/header.html",
|
|
}
|
|
tmpl, err := template.ParseFiles(files...)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
var clans []models.Clan
|
|
models.DB.Find(&clans)
|
|
|
|
data := map[string]interface{}{
|
|
"clans": clans,
|
|
}
|
|
|
|
err = tmpl.Execute(c.Writer, data)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func loginPage(c *gin.Context) {
|
|
session, _ := store.Get(c.Request, LoginSessionName)
|
|
|
|
if auth, ok := session.Values["authenticated"].(bool); ok && auth {
|
|
c.Redirect(http.StatusFound, "/")
|
|
return
|
|
}
|
|
|
|
tmpl, err := template.ParseFiles([]string{"./templates/login.html", "./templates/components/header.html"}...)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
err = tmpl.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, _ := store.Get(c.Request, 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, _ := store.Get(c.Request, 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")
|
|
}
|