53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fiber-sessions/app/handlers/usersession"
|
|
"fiber-sessions/app/templates"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
var pcLogin templates.PageContext = templates.PageContext{
|
|
Title: "Login",
|
|
ActivePage: "login",
|
|
}
|
|
|
|
func Login(ctx *fiber.Ctx) error {
|
|
pcLogin.RequestSession = usersession.NewRequestSession(ctx)
|
|
Render(ctx, templates.RenderLogin(pcLogin))
|
|
return nil
|
|
}
|
|
|
|
func LoginSubmit(ctx *fiber.Ctx) error {
|
|
requestSession := usersession.NewRequestSession(ctx)
|
|
|
|
content := templates.LoginFormValidationContent{
|
|
Validated: true,
|
|
Username: ctx.FormValue("username"),
|
|
Password: ctx.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 {
|
|
err := requestSession.LoginUser(content.Username, content.Password)
|
|
if err == nil {
|
|
ctx.Set("HX-Location", "/")
|
|
return nil
|
|
}
|
|
content.LoginError = "Invalid username or password"
|
|
}
|
|
|
|
Render(ctx, templates.LoginFormContent(content))
|
|
return nil
|
|
}
|