filegate/web/ui/login.go

58 lines
1.5 KiB
Go

package ui
import (
"log"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"git.jmbit.de/filegate/filegate/db"
"git.jmbit.de/filegate/filegate/utils"
"git.jmbit.de/filegate/filegate/web/auth"
"git.jmbit.de/filegate/filegate/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 {
user, err := db.GetUserByName(username)
if err != nil {
log.Printf("Could not retrieve User Object for User with name %s", username)
}
session.Set("username", username)
session.Set("isLoggedIn", true)
session.Set("isAdmin", user.Admin)
session.Set("isAgent", user.Agent)
err = session.Save()
if err != nil {
log.Println("[ERRO] Could not save Session")
}
c.Redirect(http.StatusTemporaryRedirect, "/")
}
}
func getLogout(c *gin.Context) {
session := sessions.Default(c)
username := session.Get("username")
session.Clear()
log.Printf("[INFO] Logged out %s", username)
c.Redirect(http.StatusTemporaryRedirect, "/login.html")
}