Handle redirection on login page
This commit is contained in:
parent
3d986c4ee2
commit
43a40df901
3 changed files with 33 additions and 8 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue