You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
829 B
Go

2 years ago
package routes
import (
"fmt"
"github.com/aziis98/lupus-lite/handlers"
2 years ago
"github.com/aziis98/lupus-lite/model"
"github.com/gofiber/fiber/v2"
)
const UserKey = "github.com/aziis98/lupus-lite/user"
func requestUser(c *fiber.Ctx) *model.User {
user, ok := c.Locals(UserKey).(*model.User) // TODO: Check semantics of casting "nil" to pointer to struct
if !ok {
return nil
}
return user
2 years ago
}
func RequireLogged(server handlers.Handler) fiber.Handler {
2 years ago
return func(c *fiber.Ctx) error {
token := c.Cookies("sid")
if token == "" {
return fmt.Errorf(`request has no session token`)
}
user, err := server.UserForSession(token)
2 years ago
if err != nil {
return err
}
// this is wrapped in a pointer to let "nil" mean no user logged in when calling "requestUser()"
2 years ago
c.Locals(UserKey, &user)
return c.Next()
}
}