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.
39 lines
703 B
Go
39 lines
703 B
Go
package routes
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/aziis98/lupus-lite/auth"
|
|
"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 {
|
|
return c.Locals(UserKey).(*model.User)
|
|
}
|
|
|
|
func RequireLoggedMiddleware(auth auth.AuthService) fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
token := c.Cookies("sid")
|
|
if token == "" {
|
|
return fmt.Errorf(`request has no session token`)
|
|
}
|
|
|
|
username, err := auth.UserForSession(token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
user, err := auth.GetUser(username)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.Locals(UserKey, &user)
|
|
|
|
return c.Next()
|
|
}
|
|
}
|