add templates for kontor-gin
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s

This commit is contained in:
Thomas Peetz
2026-09-21 19:55:46 +02:00
parent 652aa3005b
commit f2f4a0aaf6
28 changed files with 787 additions and 41 deletions
+93
View File
@@ -0,0 +1,93 @@
package controllers
import (
"kontor-api-gin/pkg/globals"
"kontor-api-gin/pkg/helpers"
"log"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
func LoginGetHandler(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(globals.UserKey)
if user != nil {
c.HTML(http.StatusBadRequest, "login.tmpl", gin.H{
"title": "Kontor Gin",
"version": "0.4.0",
"content": "Please logout first",
"user": user,
})
return
}
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"title": "Kontor Gin",
"version": "0.4.0",
"content": "",
"user": user,
})
}
func LoginPostHandler(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(globals.UserKey)
if user != nil {
c.HTML(http.StatusBadRequest, "login.tmpl", gin.H{
"title": "Kontor Gin",
"version": "0.4.0",
"content": "Please logout first",
})
return
}
username := c.PostForm("username")
password := c.PostForm("password")
if helpers.EmptyUserPass(username, password) {
c.HTML(http.StatusBadRequest, "login.tmpl", gin.H{
"title": "Kontor Gin",
"version": "0.4.0",
"content": "Parameters can't be empty",
})
}
if !helpers.CheckUserPass(username, password) {
c.HTML(http.StatusUnauthorized, "login.tmpl", gin.H{
"title": "Kontor Gin",
"version": "0.4.0",
"content": "Incorrect username or password",
})
}
session.Set(globals.UserKey, username)
if err := session.Save(); err != nil {
c.HTML(http.StatusInternalServerError, "login.tmpl", gin.H{
"title": "Kontor Gin",
"version": "0.4.0",
"content": "Failed to save session",
})
return
}
c.Redirect(http.StatusMovedPermanently, "/")
}
func LogoutGetHandler(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(globals.UserKey)
log.Printf("logging out user %v", user)
if user == nil {
log.Println("Invalid session token")
return
}
session.Delete(globals.UserKey)
if err := session.Save(); err != nil {
log.Printf("Failed to save session: %v", err)
return
}
c.Redirect(http.StatusMovedPermanently, "/")
}