https://supertokens.com/ logo
Title
g

ggishant

10/06/2022, 9:31 AM
It's not triggerring after
signinup/code/consume
api is successful? Am I doing something wrong?
n

nkshah2

10/06/2022, 9:33 AM
Hi @ggishant , The signinup post API does not trigger for the consume code flow. @sattvikc can help you with the snippet for the actual override
Also note that you do not need to init the thirdparty recipe separately if you are using thirdpartypasswordless
g

ggishant

10/06/2022, 9:36 AM
So I can use something like this?
Basically what I'm trying to achieve is, although using different signup/in methods, I want to map the user to one common user in my DB using email as the primary identifier. I'd want to map the user id to this method as well.
Hi @sattvikc , also is there a way to get user's by email from the supertoken db or just merge the passwordless auth with social auth?
n

nkshah2

10/06/2022, 10:05 AM
Ah right, so account linking as a feature is something we are working on at the moment. In the meantime one way to do it is to override the consume code and sign in up methods and check if a user already exists with that email (you can do this by mapping it yourself or using our helper function to get a user by email), then you can use the userContext feature to pass the user id to the function that creates the session
@sattvikc Can we share a quick example of what the code would look like for this?
s

sattvikc

10/06/2022, 10:06 AM
yea sure
give me a min, going thru the conversation once again
will share an example in a little while.. m on it
g

ggishant

10/06/2022, 10:19 AM
So I'm overriding the
ConsumeCodePOST
method, how do I get the user's email here from the parameters?
s

sattvikc

10/06/2022, 10:20 AM
in the API overrides, you can get email after calling the original implementation, from the OK result
g

ggishant

10/06/2022, 10:35 AM
okay thanks!
s

sattvikc

10/06/2022, 10:52 AM
you could override the recipe functions as follows to achieve what you need:
Override: &tplmodels.OverrideStruct{
    Functions: func(originalImplementation tplmodels.RecipeInterface) tplmodels.RecipeInterface {
        oConsumeCode := *originalImplementation.ConsumeCode
        nConsumeCode := func(userInput *plessmodels.UserInputCodeWithDeviceID, linkCode *string, preAuthSessionID string, userContext supertokens.UserContext) (tplmodels.ConsumeCodeResponse, error) {
            res, err := oConsumeCode(userInput, linkCode, preAuthSessionID, userContext)
            if err != nil {
                return res, err
            }
            if res.OK != nil {
                if res.OK.CreatedNewUser {
                    externalUserID := getExternalUserIDForEmail(res.OK.User.Email)
                    supertokens.CreateUserIdMapping(res.OK.User.ID, externalUserID, nil, nil)
                }
            }
            return res, err
        }
        *originalImplementation.ConsumeCode = nConsumeCode

        oThirdPartySignInUp := *originalImplementation.ThirdPartySignInUp
        nThirdPartySignInUp := func(thirdPartyID string, thirdPartyUserID string, email string, userContext supertokens.UserContext) (tplmodels.ThirdPartySignInUp, error) {
            res, err := oThirdPartySignInUp(thirdPartyID, thirdPartyUserID, email, userContext)
            if err != nil {
                return res, err
            }
            if res.OK != nil {
                if res.OK.CreatedNewUser {
                    externalUserID := getExternalUserIDForEmail(res.OK.User.Email)
                    supertokens.CreateUserIdMapping(res.OK.User.ID, externalUserID, nil, nil)
                }
            }
            return res, err
        }
        *originalImplementation.ThirdPartySignInUp = nThirdPartySignInUp
        return originalImplementation
    },
},
you will need to implement
getExternalUserIDForEmail
to get or create user ID for each email on your db.
we are also working on the Account linking feature to make these use cases possible with our SDKs.
g

ggishant

10/06/2022, 11:01 AM
Thanks so much for this!!!
Also, what argument does the
GetUserIdMapping
function take? My (External) User ID or the Supertoken User ID?
s

sattvikc

10/06/2022, 11:03 AM
either of them
it has userIdType parameter which can be one of these
@ggishant seems like what I suggested isn't the right solution, give me a little while, I'll get back
g

ggishant

10/06/2022, 11:13 AM
sure sure! 👍
s

sattvikc

10/06/2022, 11:25 AM
there is a similar situation here - https://discord.com/channels/603466164219281420/1013756565561557052/1014096025268535317 - would u be able to try this approach?