Skip to content

Commit

Permalink
adds get user recipe function
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Dec 12, 2023
1 parent 02d158e commit 5cfb55d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
52 changes: 52 additions & 0 deletions recipe/emailpassword/accountlinkingRecipeImplementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,55 @@ func TestGetNewestUsersFirstWithSearchParams(t *testing.T) {
assert.Len(t, paginationResult.Users[0].ThirdParty, 0)
}
}

func TestGetUser(t *testing.T) {
BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()
telemetry := false
supertokens.Init(supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: "http://localhost:8080",
},
AppInfo: supertokens.AppInfo{
AppName: "Testing",
Origin: "http://localhost:3000",
APIDomain: "http://localhost:3001",
},
Telemetry: &telemetry,
RecipeList: []supertokens.Recipe{
Init(nil),
},
})

ogUser, err := SignUp("public", "[email protected]", "testPass123")
if err != nil {
t.Error(err)
return
}

user, err := supertokens.GetUser(ogUser.OK.User.ID)
if err != nil {
t.Error(err)
return
}

assert.Equal(t, user.ID, ogUser.OK.User.ID)
assert.Equal(t, user.Emails[0], "[email protected]")
assert.Len(t, user.LoginMethods, 1)
assert.Len(t, user.Emails, 1)
assert.Len(t, user.PhoneNumbers, 0)
assert.Len(t, user.ThirdParty, 0)
email := "[email protected]"
assert.True(t, user.LoginMethods[0].HasSameEmailAs(&email))
assert.Equal(t, user.ID, user.LoginMethods[0].RecipeUserID.GetAsString())
assert.Equal(t, supertokens.EmailPasswordRID, user.LoginMethods[0].RecipeID)

user, err = supertokens.GetUser("random")
if err != nil {
t.Error(err)
return
}

assert.Nil(t, user)
}
30 changes: 30 additions & 0 deletions supertokens/accountlinkingRecipeImplementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,38 @@ func makeRecipeImplementation(querier Querier) AccountLinkingRecipeInterface {
return result, nil
}

getUser := func(userId string, userContext UserContext) (*User, error) {
requestBody := map[string]string{
"userId": userId,
}
resp, err := querier.SendGetRequest("/user/id", requestBody, userContext)

if err != nil {
return nil, err
}

if resp["status"].(string) != "OK" {
return nil, nil
}

var result = User{}

temporaryVariable, err := json.Marshal(resp["user"])
if err != nil {
return nil, err
}

err = json.Unmarshal(temporaryVariable, &result)
if err != nil {
return nil, err
}

return &result, nil
}

// TODO:...
return AccountLinkingRecipeInterface{
GetUsersWithSearchParams: &getUsers,
GetUser: &getUser,
}
}
13 changes: 13 additions & 0 deletions supertokens/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ func GetUsersNewestFirst(tenantId string, paginationToken *string, limit *int, i
return (*accountLinkingInstance.RecipeImpl.GetUsersWithSearchParams)(tenantId, "DESC", paginationToken, limit, includeRecipeIds, query, userContext[0])
}

func GetUser(userId string, userContext ...UserContext) (*User, error) {
accountLinkingInstance, err := getAccountLinkingRecipeInstanceOrThrowError()
if err != nil {
return nil, err
}

if len(userContext) == 0 {
userContext = append(userContext, &map[string]interface{}{})
}

return (*accountLinkingInstance.RecipeImpl.GetUser)(userId, userContext[0])
}

func DeleteUser(userId string) error {
return deleteUser(userId)
}
Expand Down

0 comments on commit 5cfb55d

Please sign in to comment.