43 lines
1 KiB
Go
43 lines
1 KiB
Go
package ui
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"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(templates.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 {
|
|
c.Errors = append(c.Errors, &gin.Error{Err: err, Type: gin.ErrorTypeAny})
|
|
metaContent := templates.GenMetaContent(c)
|
|
c.HTML(http.StatusUnauthorized, "", templates.Login(metaContent, "Login"))
|
|
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")
|
|
}
|