Passed
Push — main ( 3ec306...c5cfaa )
by Yume
01:53 queued 44s
created

controllers.GetUserFromContext   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
nop 1
1
package controllers
2
3
import (
4
	"github.com/gofiber/fiber/v2"
5
	"github.com/memnix/memnix-rest/domain"
6
	"github.com/pkg/errors"
7
	"github.com/uptrace/opentelemetry-go-extra/otelzap"
8
)
9
10
// GetUserFromContext gets the user from the context
11
func GetUserFromContext(ctx *fiber.Ctx) (domain.User, error) {
12
	if ctx.Locals("user") == nil {
13
		otelzap.Ctx(ctx.UserContext()).Error("User not found in context")
14
		return domain.User{}, errors.New("User is not initialized")
15
	}
16
	return ctx.Locals("user").(domain.User), nil
17
}
18
19
// SetUserToContext sets the user to the context
20
func SetUserToContext(ctx *fiber.Ctx, user domain.User) {
21
	ctx.Locals("user", user)
22
}
23