Login form
This commit is contained in:
47
app/handlers/login.go
Normal file
47
app/handlers/login.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user