-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_context.go
More file actions
38 lines (31 loc) · 1015 Bytes
/
auth_context.go
File metadata and controls
38 lines (31 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package handler
import (
"context"
"errors"
"strings"
"github.com/generate/selfserve/internal/errs"
"github.com/generate/selfserve/internal/models"
"github.com/gofiber/fiber/v2"
)
type authUserLookup interface {
FindUser(ctx context.Context, id string) (*models.User, error)
}
// userIDAndHotelFromAuth resolves the Clerk subject from JWT locals and loads the user's hotel_id.
func userIDAndHotelFromAuth(c *fiber.Ctx, users authUserLookup) (clerkID, hotelID string, err error) {
raw := c.Locals("userId")
clerkID, _ = raw.(string)
if strings.TrimSpace(clerkID) == "" {
return "", "", errs.Unauthorized()
}
u, ferr := users.FindUser(c.Context(), clerkID)
if ferr != nil {
if errors.Is(ferr, errs.ErrNotFoundInDB) {
return "", "", errs.BadRequest("user is not registered; complete sign-up first")
}
return "", "", errs.InternalServerError()
}
if strings.TrimSpace(u.HotelID) == "" {
return "", "", errs.BadRequest("user has no hotel assigned")
}
return clerkID, u.HotelID, nil
}