From 5cfb55de4167758beb42a6f7fc9d74b3f236b81c Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Tue, 12 Dec 2023 19:36:07 +0530 Subject: [PATCH] adds get user recipe function --- ...accountlinkingRecipeImplementation_test.go | 52 +++++++++++++++++++ .../accountlinkingRecipeImplementation.go | 30 +++++++++++ supertokens/main.go | 13 +++++ 3 files changed, 95 insertions(+) diff --git a/recipe/emailpassword/accountlinkingRecipeImplementation_test.go b/recipe/emailpassword/accountlinkingRecipeImplementation_test.go index b42fc8eb..adb533db 100644 --- a/recipe/emailpassword/accountlinkingRecipeImplementation_test.go +++ b/recipe/emailpassword/accountlinkingRecipeImplementation_test.go @@ -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", "test@gmail.com", "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], "test@gmail.com") + 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 := "test@gmail.com" + 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) +} diff --git a/supertokens/accountlinkingRecipeImplementation.go b/supertokens/accountlinkingRecipeImplementation.go index 544c2d60..939af132 100644 --- a/supertokens/accountlinkingRecipeImplementation.go +++ b/supertokens/accountlinkingRecipeImplementation.go @@ -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, } } diff --git a/supertokens/main.go b/supertokens/main.go index a767c73b..ce8582cc 100644 --- a/supertokens/main.go +++ b/supertokens/main.go @@ -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) }