48 lines
1.0 KiB
Go
48 lines
1.0 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 && !checkUsernamePassword(content.Username, content.Password) {
|
|
content.LoginError = "Invalid username or password"
|
|
}
|
|
|
|
Render(f, templates.LoginFormContent(content))
|
|
return nil
|
|
}
|