goipam/web/ui/login.go
2024-02-22 10:18:59 +01:00

36 lines
860 B
Go

package ui
import (
"log"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"git.jmbit.de/jmb/goipam/utils"
"git.jmbit.de/jmb/goipam/web/auth"
"git.jmbit.de/jmb/goipam/web/templates"
)
func getLogin(c *gin.Context) {
c.HTML(http.StatusOK, "", templates.Login(utils.GenMetaContent(c), "Login"))
}
func postLogin(c *gin.Context) {
session := sessions.Default(c)
username := c.PostForm("username")
password := c.PostForm("password")
err := auth.CheckPassword(username, password, session)
if err != nil {
metaContent := utils.GenMetaContent(c)
metaContent.ErrorTitle = "Error"
metaContent.ErrorText = err.Error()
c.HTML(http.StatusUnauthorized, "", templates.Login(metaContent, "Login"))
log.Println(err)
return
} else {
session.Set("username", username)
c.Redirect(http.StatusTemporaryRedirect, "/")
}
}