Login / logout

This commit is contained in:
Eden Kirin
2024-02-06 15:39:55 +01:00
parent 8ecfeb71e6
commit 75cded4054
12 changed files with 239 additions and 112 deletions

View File

@ -1,9 +1,8 @@
package handlers
import (
"fiber-sessions/app/handlers/usersession"
"fiber-sessions/app/templates"
"fmt"
"time"
"github.com/gofiber/fiber/v2"
)
@ -13,37 +12,19 @@ var pcLogin templates.PageContext = templates.PageContext{
ActivePage: "login",
}
func checkUsernamePassword(username string, password string) bool {
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))
func Login(ctx *fiber.Ctx) error {
pcLogin.RequestSession = usersession.NewRequestSession(ctx)
Render(ctx, 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)
func LoginSubmit(ctx *fiber.Ctx) error {
requestSession := usersession.NewRequestSession(ctx)
content := templates.LoginFormValidationContent{
Validated: true,
Username: f.FormValue("username"),
Password: f.FormValue("password"),
Username: ctx.FormValue("username"),
Password: ctx.FormValue("password"),
}
hasError := false
@ -58,20 +39,14 @@ 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", "/")
err := requestSession.LoginUser(content.Username, content.Password)
if err == nil {
ctx.Set("HX-Location", "/")
return nil
} else {
content.LoginError = "Invalid username or password"
}
content.LoginError = "Invalid username or password"
}
Render(f, templates.LoginFormContent(content))
Render(ctx, templates.LoginFormContent(content))
return nil
}