45 lines
1.1 KiB
Go
45 lines
1.1 KiB
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", nil))
|
|
}
|
|
|
|
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", err))
|
|
log.Println(err)
|
|
return
|
|
} else {
|
|
c.Redirect(http.StatusTemporaryRedirect, "/")
|
|
}
|
|
|
|
}
|
|
|
|
func getLogout(c *gin.Context) {
|
|
session := sessions.Default(c)
|
|
username := session.Get("username")
|
|
session.Clear()
|
|
session.Save()
|
|
log.Printf("[INFO] Logged out %s", username)
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login.html")
|
|
}
|