@rivascva/dt-idl 1.1.70 → 1.1.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/go/utils/context.go +18 -0
- package/package.json +1 -1
package/go/utils/context.go
CHANGED
|
@@ -2,6 +2,8 @@ package utils
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"context"
|
|
5
|
+
"errors"
|
|
6
|
+
"fmt"
|
|
5
7
|
|
|
6
8
|
"github.com/RivasCVA/dt-idl/go/models"
|
|
7
9
|
"github.com/golang-jwt/jwt/v5"
|
|
@@ -15,3 +17,19 @@ func GetTokenFromContext(ctx context.Context) *jwt.Token {
|
|
|
15
17
|
}
|
|
16
18
|
return token
|
|
17
19
|
}
|
|
20
|
+
|
|
21
|
+
func GetUserIdFromContext(ctx context.Context) (string, error) {
|
|
22
|
+
// get the token from the context
|
|
23
|
+
token := GetTokenFromContext(ctx)
|
|
24
|
+
if token == nil {
|
|
25
|
+
return "", errors.New("token not found in the context")
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// get the subject (userId) from the token
|
|
29
|
+
userId, err := token.Claims.GetSubject()
|
|
30
|
+
if err != nil {
|
|
31
|
+
return "", fmt.Errorf("failed to get the subject from the token: %w", err)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return userId, nil
|
|
35
|
+
}
|