Request object
This commit is contained in:
@ -1,20 +1,20 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/handlers/usersession"
|
||||
"fiber-sessions/app/data"
|
||||
"fiber-sessions/app/router/requests"
|
||||
"fiber-sessions/app/templates"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func Home(ctx *fiber.Ctx) error {
|
||||
requestSession := usersession.NewRequestSession(ctx)
|
||||
request := requests.NewRequest(ctx, data.UsersStorage)
|
||||
|
||||
pc := templates.PageContext{
|
||||
Title: "Welcome to the demo",
|
||||
ActivePage: "home",
|
||||
RequestSession: requestSession,
|
||||
Title: "Welcome to the demo",
|
||||
ActivePage: "home",
|
||||
}
|
||||
Render(ctx, templates.Home(pc))
|
||||
Render(ctx, templates.Home(request, pc))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -1,91 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/data"
|
||||
"fiber-sessions/app/templates"
|
||||
"fiber-sessions/app/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var pcInteractive templates.PageContext = templates.PageContext{
|
||||
Title: "Welcome to the demo - Interactive",
|
||||
ActivePage: "interactive",
|
||||
}
|
||||
|
||||
func Interactive(f *fiber.Ctx) error {
|
||||
Render(f, templates.Interactive(pcInteractive, data.CatBreeds))
|
||||
return nil
|
||||
}
|
||||
|
||||
func InteractiveSwapContent(f *fiber.Ctx) error {
|
||||
contentIndexStr := f.Query("content")
|
||||
contentIndex, err := strconv.Atoi(contentIndexStr)
|
||||
if err != nil {
|
||||
contentIndex = 0
|
||||
}
|
||||
Render(f, templates.InteractiveSwapContent(pcInteractive, contentIndex))
|
||||
return nil
|
||||
}
|
||||
|
||||
func FilterCatBreeds(f *fiber.Ctx) error {
|
||||
breedQuery := strings.ToLower(f.Query("breed"))
|
||||
countryQuery := f.Query("country")
|
||||
|
||||
var catBreeds []types.CatBreed = []types.CatBreed{}
|
||||
|
||||
for _, cb := range data.CatBreeds {
|
||||
appendToList :=
|
||||
(len(breedQuery) > 0 &&
|
||||
strings.Contains(strings.ToLower(cb.Breed), breedQuery) ||
|
||||
len(breedQuery) == 0) &&
|
||||
((len(countryQuery) > 0 &&
|
||||
cb.Country == countryQuery) ||
|
||||
len(countryQuery) == 0)
|
||||
|
||||
if appendToList {
|
||||
catBreeds = append(catBreeds, cb)
|
||||
}
|
||||
}
|
||||
|
||||
Render(f, templates.RenderCatBreedsTable(catBreeds))
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateForm(f *fiber.Ctx) error {
|
||||
content := templates.ValidateFormContent{
|
||||
Validated: true,
|
||||
NumValue: f.FormValue("number-value"),
|
||||
StrValue: f.FormValue("string-value"),
|
||||
}
|
||||
|
||||
numValue, err := strconv.Atoi(content.NumValue)
|
||||
if err != nil {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "This is not valid number"
|
||||
}
|
||||
if !content.HasNumValueError {
|
||||
if numValue < 0 {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "Value is less than 0"
|
||||
}
|
||||
if numValue > 100 {
|
||||
content.HasNumValueError = true
|
||||
content.NumValueError = "Value is greater than 100"
|
||||
}
|
||||
}
|
||||
|
||||
if len(content.StrValue) < 5 {
|
||||
content.HasStrValueError = true
|
||||
content.StrValueError = "String length is less than 5"
|
||||
}
|
||||
if len(content.StrValue) > 10 {
|
||||
content.HasStrValueError = true
|
||||
content.StrValueError = "String length is more than 10"
|
||||
}
|
||||
|
||||
Render(f, templates.RenderInteractiveForm(content))
|
||||
return nil
|
||||
}
|
||||
@ -1,7 +1,8 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/handlers/usersession"
|
||||
"fiber-sessions/app/data"
|
||||
"fiber-sessions/app/router/requests"
|
||||
"fiber-sessions/app/templates"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@ -13,13 +14,13 @@ var pcLogin templates.PageContext = templates.PageContext{
|
||||
}
|
||||
|
||||
func Login(ctx *fiber.Ctx) error {
|
||||
pcLogin.RequestSession = usersession.NewRequestSession(ctx)
|
||||
Render(ctx, templates.RenderLogin(pcLogin))
|
||||
request := requests.NewRequest(ctx, data.UsersStorage)
|
||||
Render(ctx, templates.RenderLogin(request, pcLogin))
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoginSubmit(ctx *fiber.Ctx) error {
|
||||
requestSession := usersession.NewRequestSession(ctx)
|
||||
request := requests.NewRequest(ctx, data.UsersStorage)
|
||||
|
||||
content := templates.LoginFormValidationContent{
|
||||
Validated: true,
|
||||
@ -39,7 +40,7 @@ func LoginSubmit(ctx *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
if !hasError {
|
||||
err := requestSession.LoginUser(content.Username, content.Password)
|
||||
err := request.LoginUser(content.Username, content.Password)
|
||||
if err == nil {
|
||||
ctx.Set("HX-Location", "/")
|
||||
return nil
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/handlers/usersession"
|
||||
"fiber-sessions/app/data"
|
||||
"fiber-sessions/app/router/requests"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func Logout(ctx *fiber.Ctx) error {
|
||||
requestSession := usersession.NewRequestSession(ctx)
|
||||
requestSession.Logout()
|
||||
request := requests.NewRequest(ctx, data.UsersStorage)
|
||||
request.Logout()
|
||||
|
||||
ctx.Redirect("/")
|
||||
return nil
|
||||
|
||||
@ -1,113 +0,0 @@
|
||||
package usersession
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fiber-sessions/app/types"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"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,
|
||||
})
|
||||
|
||||
const SESSION_EXPIRES = time.Minute * 30
|
||||
|
||||
var dbUsers map[string]types.User = map[string]types.User{
|
||||
"pero-uuid": {
|
||||
Id: "pero-uuid",
|
||||
Username: "pero",
|
||||
Password: "pero",
|
||||
FirstName: "Pero",
|
||||
LastName: "Perić",
|
||||
},
|
||||
"mirko-uuid": {
|
||||
Id: "mirko-uuid",
|
||||
Username: "mirko",
|
||||
Password: "mirko",
|
||||
FirstName: "Mirko",
|
||||
LastName: "Mirković",
|
||||
},
|
||||
}
|
||||
|
||||
type RequestSession struct {
|
||||
ctx *fiber.Ctx
|
||||
session *session.Session
|
||||
User *types.User
|
||||
}
|
||||
|
||||
func NewRequestSession(ctx *fiber.Ctx) *RequestSession {
|
||||
session, err := sessionStore.Get(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var user *types.User = nil
|
||||
|
||||
userId := session.Get("userId")
|
||||
fmt.Printf("userId: %+v\n", userId)
|
||||
if userId != nil {
|
||||
user = getUserById(userId.(string))
|
||||
}
|
||||
|
||||
return &RequestSession{
|
||||
ctx: ctx,
|
||||
session: session,
|
||||
User: user,
|
||||
}
|
||||
}
|
||||
|
||||
func getUserById(id string) *types.User {
|
||||
user, ok := dbUsers[id]
|
||||
if ok {
|
||||
return &user
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getUser(username string, password string) *types.User {
|
||||
for _, user := range dbUsers {
|
||||
if user.Username == username {
|
||||
if user.Password == password {
|
||||
return &user
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RequestSession) LoginUser(username string, password string) error {
|
||||
user := getUser(username, password)
|
||||
|
||||
if user == nil {
|
||||
return errors.New("invalid username or password")
|
||||
}
|
||||
r.User = user
|
||||
|
||||
r.session.Set("userId", r.User.Id)
|
||||
r.session.SetExpiry(SESSION_EXPIRES)
|
||||
if err := r.session.Save(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RequestSession) UserAuthenticated() bool {
|
||||
return r.User != nil
|
||||
}
|
||||
|
||||
func (r *RequestSession) Logout() error {
|
||||
if !r.UserAuthenticated() {
|
||||
return errors.New("user not authenticated")
|
||||
}
|
||||
r.session.Destroy()
|
||||
r.User = nil
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user