Skip to content

Commit

Permalink
moves account linking into supertokens package
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Dec 11, 2023
1 parent e9db988 commit 5403429
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 225 deletions.
44 changes: 0 additions & 44 deletions recipe/accountlinking/accountlinkingmodels/models.go

This file was deleted.

112 changes: 44 additions & 68 deletions recipe/dashboard/api/usersGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,20 @@ import (
)

type UsersGetResponse struct {
Status string `json:"status"`
NextPaginationToken *string `json:"nextPaginationToken,omitempty"`
Users []Users `json:"users"`
Status string `json:"status"`
NextPaginationToken *string `json:"nextPaginationToken,omitempty"`
Users []UserWithFirstAndLastName `json:"users"`
}

type Users struct {
RecipeId string `json:"recipeId"`
User User `json:"user"`
type UserWithFirstAndLastName struct {
supertokens.User
firstName string
lastName string
}

type User struct {
Id string `json:"id"`
TimeJoined float64 `json:"timeJoined"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
Email string `json:"email,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
ThirdParty dashboardmodels.ThirdParty `json:"thirdParty,omitempty"`
TenantIds string `json:"tenantIds,omitempty"`
type UserPaginationResultWithFirstAndLastName struct {
Users []UserWithFirstAndLastName
NextPaginationToken *string
}

func UsersGet(apiImplementation dashboardmodels.APIInterface, tenantId string, options dashboardmodels.APIOptions, userContext supertokens.UserContext) (UsersGetResponse, error) {
Expand Down Expand Up @@ -83,7 +78,8 @@ func UsersGet(apiImplementation dashboardmodels.APIInterface, tenantId string, o
}

if len(queryParamsObject) != 0 {
usersResponse, err = supertokens.GetUsersWithSearchParams(tenantId, timeJoinedOrder, paginationTokenPtr, &limit, nil, queryParamsObject)
// the oder here doesn't matter cause in search, we return all users anyway.
usersResponse, err = supertokens.GetUsersNewestFirst(tenantId, paginationTokenPtr, &limit, nil, queryParamsObject)
} else if timeJoinedOrder == "ASC" {
usersResponse, err = supertokens.GetUsersOldestFirst(tenantId, paginationTokenPtr, &limit, nil, nil)
} else {
Expand All @@ -93,12 +89,28 @@ func UsersGet(apiImplementation dashboardmodels.APIInterface, tenantId string, o
return UsersGetResponse{}, err
}

var userResponseWithFirstAndLastName UserPaginationResultWithFirstAndLastName = UserPaginationResultWithFirstAndLastName{}

// copy userResponse into userResponseWithFirstAndLastName
userResponseWithFirstAndLastName.NextPaginationToken = usersResponse.NextPaginationToken
for _, userObj := range usersResponse.Users {
userResponseWithFirstAndLastName.Users = append(userResponseWithFirstAndLastName.Users, struct {
supertokens.User
firstName string
lastName string
}{
User: userObj,
firstName: "",
lastName: "",
})
}

_, err = usermetadata.GetRecipeInstanceOrThrowError()
if err != nil {
return UsersGetResponse{
Status: "OK",
NextPaginationToken: usersResponse.NextPaginationToken,
Users: getUsersTypeFromPaginationResult(usersResponse),
Users: userResponseWithFirstAndLastName.Users,
}, nil
}

Expand All @@ -109,26 +121,33 @@ func UsersGet(apiImplementation dashboardmodels.APIInterface, tenantId string, o
var sem = make(chan int, batchSize)
var errInBackground error

for i, userObj := range usersResponse.Users {
for i, userObj := range userResponseWithFirstAndLastName.Users {
sem <- 1

if errInBackground != nil {
return UsersGetResponse{}, errInBackground
}

go func(i int, userObj struct {
RecipeId string `json:"recipeId"`
User map[string]interface{} `json:"user"`
supertokens.User
firstName string
lastName string
}) {
defer processingGroup.Done()
userMetadataResponse, err := usermetadata.GetUserMetadata(userObj.User["id"].(string), userContext)
userMetadataResponse, err := usermetadata.GetUserMetadata(userObj.ID, userContext)
<-sem
if err != nil {
errInBackground = err
return
}
usersResponse.Users[i].User["firstName"] = userMetadataResponse["first_name"]
usersResponse.Users[i].User["lastName"] = userMetadataResponse["last_name"]
firstName, ok := userMetadataResponse["first_name"]
lastName, ok2 := userMetadataResponse["last_name"]
if ok {
userResponseWithFirstAndLastName.Users[i].firstName = firstName.(string)
}
if ok2 {
userResponseWithFirstAndLastName.Users[i].lastName = lastName.(string)
}
}(i, userObj)
}

Expand All @@ -140,50 +159,7 @@ func UsersGet(apiImplementation dashboardmodels.APIInterface, tenantId string, o

return UsersGetResponse{
Status: "OK",
NextPaginationToken: usersResponse.NextPaginationToken,
Users: getUsersTypeFromPaginationResult(usersResponse),
NextPaginationToken: userResponseWithFirstAndLastName.NextPaginationToken,
Users: userResponseWithFirstAndLastName.Users,
}, nil
}

func getUsersTypeFromPaginationResult(usersResponse supertokens.UserPaginationResult) []Users {
users := []Users{}
for _, v := range usersResponse.Users {
user := User{
Id: v.User["id"].(string),
TimeJoined: v.User["timeJoined"].(float64),
}
firstName := v.User["firstName"]
if firstName != nil {
user.FirstName = firstName.(string)
}
lastName := v.User["lastName"]
if lastName != nil {
user.LastName = lastName.(string)
}

if v.RecipeId == "emailpassword" {
user.Email = v.User["email"].(string)
} else if v.RecipeId == "thirdparty" {
user.Email = v.User["email"].(string)
user.ThirdParty = dashboardmodels.ThirdParty{
Id: v.User["thirdParty"].(map[string]interface{})["id"].(string),
UserId: v.User["thirdParty"].(map[string]interface{})["userId"].(string),
}
} else {
email := v.User["email"]
if email != nil {
user.Email = email.(string)
}
phoneNumber := v.User["phoneNumber"]
if phoneNumber != nil {
user.PhoneNumber = phoneNumber.(string)
}
}

users = append(users, Users{
RecipeId: v.RecipeId,
User: user,
})
}
return users
}
13 changes: 7 additions & 6 deletions recipe/dashboard/userGet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package dashboard

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/supertokens/supertokens-golang/recipe/dashboard/api"
"github.com/supertokens/supertokens-golang/recipe/dashboard/api/userdetails"
"github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
"github.com/supertokens/supertokens-golang/recipe/session"
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
"github.com/supertokens/supertokens-golang/recipe/thirdpartypasswordless"
"github.com/supertokens/supertokens-golang/recipe/thirdpartypasswordless/tplmodels"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels"
Expand Down Expand Up @@ -145,7 +146,7 @@ func TestThatUserGetReturnsValidUserForThirdPartyUserWhenUsingThirdPartyPassword

user := listResponse.Users[0].User

req, err = http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard/api/user?userId="+user.Id+"&recipeId=thirdparty", strings.NewReader(`{}`))
req, err = http.NewRequest(http.MethodGet, testServer.URL+"/auth/dashboard/api/user?userId="+user.ID+"&recipeId=thirdparty", strings.NewReader(`{}`))
req.Header.Set("Authorization", "Bearer testapikey")
res, err = http.DefaultClient.Do(req)

Expand Down
4 changes: 2 additions & 2 deletions recipe/emailpassword/deleteUser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func TestDeleteUser(t *testing.T) {
if err != nil {
t.Error(err.Error())
}
reponseAfterDeletingUser, err := supertokens.GetUsersOldestFirst("public", nil, nil, nil, nil)
responseAfterDeletingUser, err := supertokens.GetUsersOldestFirst("public", nil, nil, nil, nil)
if err != nil {
t.Error(err.Error())
}
assert.Equal(t, 0, len(reponseAfterDeletingUser.Users))
assert.Equal(t, 0, len(responseAfterDeletingUser.Users))
}
}
2 changes: 1 addition & 1 deletion recipe/emailpassword/userIdMapping_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ func TestCreateUserIdMappingAndGetUsers(t *testing.T) {
assert.NotNil(t, userResult.Users)

for i, user := range userResult.Users {
assert.Equal(t, user.User["id"], fmt.Sprintf("externalId%d", i))
assert.Equal(t, user.ID, fmt.Sprintf("externalId%d", i))
}
}
8 changes: 4 additions & 4 deletions recipe/emailpassword/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestGetUsersOldestFirst(t *testing.T) {

assert.Equal(t, len(users.Users), 1)
assert.NotNil(t, users.NextPaginationToken)
assert.Equal(t, "[email protected]", users.Users[0].User["email"])
assert.Equal(t, "[email protected]", users.Users[0].Emails[0])

users, err = supertokens.GetUsersOldestFirst("public", users.NextPaginationToken, &limit, nil, nil)
if err != nil {
Expand All @@ -106,7 +106,7 @@ func TestGetUsersOldestFirst(t *testing.T) {

assert.Equal(t, len(users.Users), 1)
assert.NotNil(t, users.NextPaginationToken)
assert.Equal(t, "[email protected]", users.Users[0].User["email"])
assert.Equal(t, "[email protected]", users.Users[0].Emails[0])

limit = 5
users, err = supertokens.GetUsersOldestFirst("public", users.NextPaginationToken, &limit, nil, nil)
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestGetUsersNewestFirst(t *testing.T) {

assert.Equal(t, len(users.Users), 1)
assert.NotNil(t, users.NextPaginationToken)
assert.Equal(t, "[email protected]", users.Users[0].User["email"])
assert.Equal(t, "[email protected]", users.Users[0].Emails[0])

users, err = supertokens.GetUsersNewestFirst("public", users.NextPaginationToken, &limit, nil, nil)
if err != nil {
Expand All @@ -212,7 +212,7 @@ func TestGetUsersNewestFirst(t *testing.T) {

assert.Equal(t, len(users.Users), 1)
assert.NotNil(t, users.NextPaginationToken)
assert.Equal(t, "[email protected]", users.Users[0].User["email"])
assert.Equal(t, "[email protected]", users.Users[0].Emails[0])

limit = 5
users, err = supertokens.GetUsersNewestFirst("public", users.NextPaginationToken, &limit, nil, nil)
Expand Down
8 changes: 4 additions & 4 deletions recipe/thirdparty/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ func TestGetUsersOldesFirst(t *testing.T) {
t.Error(err.Error())
}
assert.Equal(t, 1, len(userPaginationResult.Users))
assert.Equal(t, "[email protected]", userPaginationResult.Users[0].User["email"])
assert.Equal(t, "[email protected]", userPaginationResult.Users[0].Emails[0])
assert.Equal(t, "*string", reflect.TypeOf(userPaginationResult.NextPaginationToken).String())

userPaginationResult, err = supertokens.GetUsersOldestFirst("public", userPaginationResult.NextPaginationToken, &customLimit, nil, nil)
if err != nil {
t.Error(err.Error())
}
assert.Equal(t, 1, len(userPaginationResult.Users))
assert.Equal(t, "[email protected]", userPaginationResult.Users[0].User["email"])
assert.Equal(t, "[email protected]", userPaginationResult.Users[0].Emails[0])
assert.Equal(t, "*string", reflect.TypeOf(userPaginationResult.NextPaginationToken).String())

customLimit = 5
Expand Down Expand Up @@ -215,15 +215,15 @@ func TestGetUsersNewestFirst(t *testing.T) {
t.Error(err.Error())
}
assert.Equal(t, 1, len(userPaginationResult.Users))
assert.Equal(t, "[email protected]", userPaginationResult.Users[0].User["email"])
assert.Equal(t, "[email protected]", userPaginationResult.Users[0].Emails[0])
assert.Equal(t, "*string", reflect.TypeOf(userPaginationResult.NextPaginationToken).String())

userPaginationResult, err = supertokens.GetUsersNewestFirst("public", userPaginationResult.NextPaginationToken, &customLimit, nil, nil)
if err != nil {
t.Error(err.Error())
}
assert.Equal(t, 1, len(userPaginationResult.Users))
assert.Equal(t, "[email protected]", userPaginationResult.Users[0].User["email"])
assert.Equal(t, "[email protected]", userPaginationResult.Users[0].Emails[0])
assert.Equal(t, "*string", reflect.TypeOf(userPaginationResult.NextPaginationToken).String())

customLimit = 5
Expand Down
Loading

0 comments on commit 5403429

Please sign in to comment.