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
40 lines
829 B
Go
package routes
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/aziis98/lupus-lite/handlers"
|
|
"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
|
|
}
|
|
|
|
func RequireLogged(server handlers.Handler) fiber.Handler {
|
|
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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// this is wrapped in a pointer to let "nil" mean no user logged in when calling "requestUser()"
|
|
c.Locals(UserKey, &user)
|
|
|
|
return c.Next()
|
|
}
|
|
}
|