Handle redirection on login page

This commit is contained in:
William Bouzourène 2024-12-21 11:00:32 +01:00
parent 7cec3bb263
commit dc8e62ddc7
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww
3 changed files with 33 additions and 8 deletions

View file

@ -1,8 +1,11 @@
package middlewares
import (
"fmt"
"git.readonly.ch/bouzoure/popvaud-people/helpers"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)
func AuthMiddleware(c *fiber.Ctx) error {
@ -15,19 +18,39 @@ func AuthMiddleware(c *fiber.Ctx) error {
return err
}
denyAccess := false
userid := sess.Get("userid")
if userid == nil {
return c.Redirect("/login")
denyAccess = true
} else {
active, err := helpers.UserExistsAndIsActive(userid.(int))
if err != nil {
return err
}
if !active {
denyAccess = true
}
}
active, err := helpers.UserExistsAndIsActive(userid.(int))
if err != nil {
return err
}
if denyAccess {
if c.Path() == "/" {
return c.Redirect("/login")
}
if !active {
return c.Redirect("/login")
id := uuid.NewString()
key := fmt.Sprintf("redirect-%s", id)
sess.Set(key, c.Path())
sess.Save()
redirectUrl := fmt.Sprintf(
"/login?redirect=%s",
id,
)
return c.Redirect(redirectUrl)
}
return c.Next()