Files
fiber-sessions/app/handlers/login.go
2024-02-06 08:36:24 +01:00

53 lines
1.1 KiB
Go

package handlers
import (
"fiber-sessions/app/templates"
"github.com/gofiber/fiber/v2"
)
var pcLogin templates.PageContext = templates.PageContext{
Title: "Login",
ActivePage: "login",
}
func checkUsernamePassword(username string, password string) bool {
return username == "pero" && password == "pero"
}
func Login(f *fiber.Ctx) error {
Render(f, templates.RenderLogin(pcLogin))
return nil
}
func LoginSubmit(f *fiber.Ctx) error {
content := templates.LoginFormValidationContent{
Validated: true,
Username: f.FormValue("username"),
Password: f.FormValue("password"),
}
hasError := false
if len(content.Username) == 0 {
content.UsernameError = "Username can not be empty."
hasError = true
}
if len(content.Password) == 0 {
content.PasswordError = "Password can not be empty."
hasError = true
}
if !hasError {
if checkUsernamePassword(content.Username, content.Password) {
f.Set("HX-Location", "/")
return nil
} else {
content.LoginError = "Invalid username or password"
}
}
Render(f, templates.LoginFormContent(content))
return nil
}