Skip to main content

Get User Info

There are several ways to fetch information about a user:

  • Using their user ID, you can get their email ID, time joined, and metadata that is saved in the user metadata recipe.
  • If the user logged in via a third party providers, you can get their profile info in the post sign up override along with the provider's access token. You can save this information in the user metadata recipe for later retrieval.
  • Lastly, you can get the user's session information and access token payload from their session handle (offline mode), or from the currently logged in session object (online mode).

Fetching information using the user's email#

You can get a user's information on the backend using the getUsersByEmail and getUserById functions:

import ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";
async function getUserInfo() {    // Note that usersInfo has type User[]    // You can learn more about the `User` object over here https://github.com/supertokens/core-driver-interface/wiki    let usersInfo = await ThirdPartyEmailPassword.getUsersByEmail("test@example.com");}

Fetching information using the user's ID#

Using the getUserById function#

import express from "express";import ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";import { verifySession } from "supertokens-node/recipe/session/framework/express";import { SessionRequest } from 'supertokens-node/framework/express';
let app = express();app.get("/get-user-info", verifySession(), async (req: SessionRequest, res) => {    let userId = req.session!.getUserId();    // You can learn more about the `User` object over here https://github.com/supertokens/core-driver-interface/wiki    let userInfo = await ThirdPartyEmailPassword.getUserById(userId)    // ... })

Using the user metadata recipe#

Checkout the user metadata recipe docs which shows you how to save and fetch any JSON object against the user's ID. You can use this to save information like the user's name (first_name and last_name) or any other field associated with the user.

Getting information from the user's session#

The user's session contains their user ID and the session's payload. You can access this on the backend and frontend as well as whilst the user is online or offline.

More information about this can be found in the session docs.

Getting the user's third party provider information and access token#

If the user used a third party provider to login, you can access their info via SuperTokens as shown below. You can then save the OAuthTokens in your own db or in SuperTokens (using the user metadata recipe) and use them to fetch / change info about the logged in user from the third party provider

import SuperTokens from "supertokens-node";import ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";import Session from "supertokens-node/recipe/session";
SuperTokens.init({    appInfo: {        apiDomain: "...",        appName: "...",        websiteDomain: "..."    },    supertokens: {         connectionURI: "...",     },    recipeList: [        ThirdPartyEmailPassword.init({            override: {                functions: (originalImplementation) => {                    return {                        ...originalImplementation,                        // override the thirdparty sign in / up API                        thirdPartySignInUpPOST: async function(input) {                            if (originalImplementation.thirdPartySignInUpPOST === undefined) {                                throw Error("Should never come here");                            }
                            // TODO: Some pre sign in / up logic
                            let response = await originalImplementation.thirdPartySignInUpPOST(input);
                            if (response.status === "OK") {                                if (response.createdNewUser) {                                    // TODO: some post sign up logic                                } else {                                    // TODO: some post sign in logic                                }                            }
                            return response;                        }                    }                }            }        }),        Session.init({ /* ... */ })    ]});