Begin sessions

This commit is contained in:
Eden Kirin
2024-02-06 09:33:09 +01:00
parent 5c8f2b754f
commit 8ecfeb71e6
6 changed files with 42 additions and 3 deletions

View File

@ -2,6 +2,8 @@ package handlers
import (
"fiber-sessions/app/templates"
"fmt"
"time"
"github.com/gofiber/fiber/v2"
)
@ -12,15 +14,32 @@ var pcLogin templates.PageContext = templates.PageContext{
}
func checkUsernamePassword(username string, password string) bool {
return username == "pero" && password == "pero"
return true
// return username == "pero" && password == "pero"
}
func Login(f *fiber.Ctx) error {
session, err := sessionStore.Get(f)
if err != nil {
panic("Error gettings sessionStore: " + err.Error())
}
userId := session.Get("userId")
fmt.Printf("userId: %+v\n", userId)
Render(f, templates.RenderLogin(pcLogin))
return nil
}
func LoginSubmit(f *fiber.Ctx) error {
session, err := sessionStore.Get(f)
if err != nil {
panic("Error gettings sessionStore: " + err.Error())
}
userId := session.Get("userId")
fmt.Printf("userId: %+v\n", userId)
content := templates.LoginFormValidationContent{
Validated: true,
Username: f.FormValue("username"),
@ -40,6 +59,12 @@ func LoginSubmit(f *fiber.Ctx) error {
if !hasError {
if checkUsernamePassword(content.Username, content.Password) {
session.Set("userId", "neki-user-id")
session.SetExpiry(time.Second * 60)
if err := session.Save(); err != nil {
panic(err)
}
f.Set("HX-Location", "/")
return nil
} else {