Begin sessions
This commit is contained in:
@ -2,6 +2,8 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fiber-sessions/app/templates"
|
"fiber-sessions/app/templates"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
@ -12,15 +14,32 @@ var pcLogin templates.PageContext = templates.PageContext{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkUsernamePassword(username string, password string) bool {
|
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 {
|
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))
|
Render(f, templates.RenderLogin(pcLogin))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoginSubmit(f *fiber.Ctx) error {
|
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{
|
content := templates.LoginFormValidationContent{
|
||||||
Validated: true,
|
Validated: true,
|
||||||
Username: f.FormValue("username"),
|
Username: f.FormValue("username"),
|
||||||
@ -40,6 +59,12 @@ func LoginSubmit(f *fiber.Ctx) error {
|
|||||||
|
|
||||||
if !hasError {
|
if !hasError {
|
||||||
if checkUsernamePassword(content.Username, content.Password) {
|
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", "/")
|
f.Set("HX-Location", "/")
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
11
app/handlers/usersession.go
Normal file
11
app/handlers/usersession.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/session"
|
||||||
|
"github.com/gofiber/storage/memory/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var sessionStorage = memory.New()
|
||||||
|
var sessionStore = session.New(session.Config{
|
||||||
|
Storage: sessionStorage,
|
||||||
|
})
|
||||||
@ -61,7 +61,7 @@ templ RenderLogin(pc PageContext) {
|
|||||||
hx-target="#login-form-container"
|
hx-target="#login-form-container"
|
||||||
hx-swap="outerHTML"
|
hx-swap="outerHTML"
|
||||||
>
|
>
|
||||||
@LoginFormContent(LoginFormValidationContent{})
|
@LoginFormContent(LoginFormValidationContent{Username: "pero", Password: "pero"})
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -216,7 +216,7 @@ func RenderLogin(pc PageContext) templ.Component {
|
|||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = LoginFormContent(LoginFormValidationContent{}).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = LoginFormContent(LoginFormValidationContent{Username: "pero", Password: "pero"}).Render(ctx, templ_7745c5c3_Buffer)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@ -9,6 +9,7 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.0.5 // indirect
|
github.com/andybalholm/brotli v1.0.5 // indirect
|
||||||
|
github.com/gofiber/storage/memory/v2 v2.0.0 // indirect
|
||||||
github.com/google/uuid v1.5.0 // indirect
|
github.com/google/uuid v1.5.0 // indirect
|
||||||
github.com/klauspost/compress v1.17.0 // indirect
|
github.com/klauspost/compress v1.17.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@ -8,6 +8,8 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1
|
|||||||
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
|
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
|
||||||
github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE=
|
github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE=
|
||||||
github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
|
github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
|
||||||
|
github.com/gofiber/storage/memory/v2 v2.0.0 h1:4Xn+Dx8mvwc+1gRgw9l1GY2qgXQOfUYV0xefDD3tBLs=
|
||||||
|
github.com/gofiber/storage/memory/v2 v2.0.0/go.mod h1:vGipSznvPX/U8waxPniNMWT+nL0hH4U8XaYZ0m30m0U=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||||
|
|||||||
Reference in New Issue
Block a user