57 lines
866 B
Go
57 lines
866 B
Go
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 {
|
|
if c.Path() == "/login" {
|
|
return c.Next()
|
|
}
|
|
|
|
sess, err := helpers.GetSessionStore(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
denyAccess := false
|
|
userid := sess.Get("userid")
|
|
|
|
if userid == nil {
|
|
denyAccess = true
|
|
} else {
|
|
active, err := helpers.UserExistsAndIsActive(userid.(uint))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !active {
|
|
denyAccess = true
|
|
}
|
|
}
|
|
|
|
if denyAccess {
|
|
if c.Path() == "/" {
|
|
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()
|
|
}
|