Login form

This commit is contained in:
Eden Kirin
2024-02-05 23:27:44 +01:00
parent c0f161abe1
commit bc368641a8
10 changed files with 375 additions and 181 deletions

47
app/handlers/login.go Normal file
View File

@ -0,0 +1,47 @@
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
}