Skip to content

Commit b19c439

Browse files
Merge pull request #382 from supertokens/fix/user-get-tenant-id
fix: Fix tenant ids being nil in user get dashboard API
2 parents ee60032 + 6a7eb4f commit b19c439

File tree

6 files changed

+99
-11
lines changed

6 files changed

+99
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10+
## [0.16.2] - 2023-10-17
11+
12+
- Fixes an issue where tenant ids returned for a user from the user get API of the dashboard recipe would always be nil for thirdpartyemailpassword and thirdpartypasswordless recipes
13+
1014
## [0.16.1] - 2023-10-03
1115

1216
### Changes

recipe/dashboard/api/userdetails/userGet.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,45 @@ import (
2424
"github.com/supertokens/supertokens-golang/supertokens"
2525
)
2626

27-
type userGetResponse struct {
27+
type UserGetResponse struct {
2828
Status string `json:"status"`
2929
RecipeId string `json:"recipeId,omitempty"`
3030
User dashboardmodels.UserType `json:"user,omitempty"`
3131
}
3232

33-
func UserGet(apiImplementation dashboardmodels.APIInterface, tenantId string, options dashboardmodels.APIOptions, userContext supertokens.UserContext) (userGetResponse, error) {
33+
func UserGet(apiImplementation dashboardmodels.APIInterface, tenantId string, options dashboardmodels.APIOptions, userContext supertokens.UserContext) (UserGetResponse, error) {
3434
req := options.Req
3535
userId := req.URL.Query().Get("userId")
3636
recipeId := req.URL.Query().Get("recipeId")
3737

3838
if userId == "" {
39-
return userGetResponse{}, supertokens.BadInputError{
39+
return UserGetResponse{}, supertokens.BadInputError{
4040
Msg: "Missing required parameter 'userId'",
4141
}
4242
}
4343

4444
if recipeId == "" {
45-
return userGetResponse{}, supertokens.BadInputError{
45+
return UserGetResponse{}, supertokens.BadInputError{
4646
Msg: "Missing required parameter 'recipeId'",
4747
}
4848
}
4949

5050
if !api.IsValidRecipeId(recipeId) {
51-
return userGetResponse{}, supertokens.BadInputError{
51+
return UserGetResponse{}, supertokens.BadInputError{
5252
Msg: "Invalid recipe id",
5353
}
5454
}
5555

5656
if !api.IsRecipeInitialised(recipeId) {
57-
return userGetResponse{
57+
return UserGetResponse{
5858
Status: "RECIPE_NOT_INITIALISED",
5959
}, nil
6060
}
6161

6262
userForRecipeId, _ := api.GetUserForRecipeId(userId, recipeId, userContext)
6363

6464
if reflect.DeepEqual(userForRecipeId, dashboardmodels.UserType{}) {
65-
return userGetResponse{
65+
return UserGetResponse{
6666
Status: "NO_USER_FOUND_ERROR",
6767
}, nil
6868
}
@@ -74,7 +74,7 @@ func UserGet(apiImplementation dashboardmodels.APIInterface, tenantId string, op
7474
userForRecipeId.FirstName = "FEATURE_NOT_ENABLED"
7575
userForRecipeId.LastName = "FEATURE_NOT_ENABLED"
7676

77-
return userGetResponse{
77+
return UserGetResponse{
7878
Status: "OK",
7979
RecipeId: recipeId,
8080
User: userForRecipeId,
@@ -84,7 +84,7 @@ func UserGet(apiImplementation dashboardmodels.APIInterface, tenantId string, op
8484
metadata, metadataerr := usermetadata.GetUserMetadata(userId, userContext)
8585

8686
if metadataerr != nil {
87-
return userGetResponse{}, metadataerr
87+
return UserGetResponse{}, metadataerr
8888
}
8989

9090
// first and last name should be an empty string if they dont exist in metadata
@@ -99,7 +99,7 @@ func UserGet(apiImplementation dashboardmodels.APIInterface, tenantId string, op
9999
userForRecipeId.LastName = metadata["last_name"].(string)
100100
}
101101

102-
return userGetResponse{
102+
return UserGetResponse{
103103
Status: "OK",
104104
RecipeId: recipeId,
105105
User: userForRecipeId,

recipe/dashboard/userGet_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package dashboard
2+
3+
import (
4+
"encoding/json"
5+
"github.com/supertokens/supertokens-golang/recipe/dashboard/api/userdetails"
6+
"io"
7+
"net/http"
8+
"net/http/httptest"
9+
"strings"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
"github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels"
14+
"github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword"
15+
"github.com/supertokens/supertokens-golang/supertokens"
16+
"github.com/supertokens/supertokens-golang/test/unittesting"
17+
)
18+
19+
/*
20+
- Initialise with thirdpartyemailpassword and provide no custom form fields
21+
- Create an emailpassword user using the thirdpartyemailpassword recipe
22+
- Get user from the user get API
23+
- Check that user has public tenant
24+
*/
25+
func TestThatUserGetReturnsTenantIDsCorrectly(t *testing.T) {
26+
config := supertokens.TypeInput{
27+
OnSuperTokensAPIError: func(err error, req *http.Request, res http.ResponseWriter) {
28+
print(err)
29+
},
30+
Supertokens: &supertokens.ConnectionInfo{
31+
ConnectionURI: "http://localhost:8080",
32+
},
33+
AppInfo: supertokens.AppInfo{
34+
APIDomain: "api.supertokens.io",
35+
AppName: "SuperTokens",
36+
WebsiteDomain: "supertokens.io",
37+
},
38+
RecipeList: []supertokens.Recipe{
39+
thirdpartyemailpassword.Init(nil),
40+
Init(&dashboardmodels.TypeInput{
41+
ApiKey: "testapikey",
42+
}),
43+
},
44+
}
45+
46+
BeforeEach()
47+
unittesting.StartUpST("localhost", "8080")
48+
defer AfterEach()
49+
err := supertokens.Init(config)
50+
if err != nil {
51+
t.Error(err.Error())
52+
}
53+
54+
mux := http.NewServeMux()
55+
testServer := httptest.NewServer(supertokens.Middleware(mux))
56+
defer testServer.Close()
57+
58+
signupResponse, err := thirdpartyemailpassword.EmailPasswordSignUp("public", "[email protected]", "abcd1234")
59+
if err != nil {
60+
t.Error(err.Error())
61+
}
62+
63+
assert.NotNil(t, signupResponse.OK)
64+
65+
userId := signupResponse.OK.User.ID
66+
67+
req, err := http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard/api/user?userId="+userId+"&recipeId=emailpassword", strings.NewReader(`{}`))
68+
req.Header.Set("Authorization", "Bearer testapikey")
69+
res, err := http.DefaultClient.Do(req)
70+
71+
if err != nil {
72+
t.Error(err.Error())
73+
}
74+
75+
var response userdetails.UserGetResponse
76+
body, _ := io.ReadAll(res.Body)
77+
json.Unmarshal(body, &response)
78+
79+
assert.True(t, len(response.User.TenantIds) > 0)
80+
assert.Equal(t, response.User.TenantIds[0], "public")
81+
}

recipe/thirdpartyemailpassword/recipeimplementation/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func MakeRecipeImplementation(emailPasswordQuerier supertokens.Querier, thirdPar
173173
ID: user.ID,
174174
Email: user.Email,
175175
TimeJoined: user.TimeJoined,
176+
TenantIds: user.TenantIds,
176177
ThirdParty: nil,
177178
}, nil
178179
}
@@ -191,6 +192,7 @@ func MakeRecipeImplementation(emailPasswordQuerier supertokens.Querier, thirdPar
191192
Email: userinfo.Email,
192193
TimeJoined: userinfo.TimeJoined,
193194
ThirdParty: &userinfo.ThirdParty,
195+
TenantIds: userinfo.TenantIds,
194196
}, nil
195197
}
196198
return nil, nil

recipe/thirdpartypasswordless/recipeimplementation/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func MakeRecipeImplementation(passwordlessQuerier supertokens.Querier, thirdPart
123123
Email: user.Email,
124124
PhoneNumber: user.PhoneNumber,
125125
TimeJoined: user.TimeJoined,
126+
TenantIds: user.TenantIds,
126127
ThirdParty: nil,
127128
}, nil
128129
}

supertokens/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
)
2222

2323
// VERSION current version of the lib
24-
const VERSION = "0.16.1"
24+
const VERSION = "0.16.2"
2525

2626
var (
2727
cdiSupported = []string{"3.0"}

0 commit comments

Comments
 (0)