authorizationuser-management

Supertokens - get userid by user email


Using supertokens (hosted), is there a direct way to query the userId of another user by email? The only solution I found was to fetch all users and search for it, which is obviously not efficient.


Solution

  • Depending on the recipe and the SDK you use, there are functions available for that. For example, with the NodeJS SDK, you have:

    EmailPassword recipe

    getUserByEmail

    ThirdParty recipe

    getUsersByEmail (notice that this will return an array of users since you can have multiple users sign up with the same email, but different OAuth providers)

    Passwordless recipe

    getUserByEmail (This function will return Promise<undefined> if the user doesn't exist, if if they had used phone number to sign up)

    ThirdPartyEmailPassword

    getUsersByEmail

    ThirdPartyPasswordless

    getUsersByEmail

    Other SDKs

    For other backend SDKs, you can check out their docs:

    Using the SuperTokens Core APIs directly

    In the API Spec, you can see sections for each recipe. Depending on how the user signed up, you can query that /recipe/user GET API with their email as a query param. Be sure to pass in the correct rid header in the GET request.