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#

Use the listUsersByAccountInfo function to retrieve a user's data, specifying the account type and providing the user's email as criteria.

import supertokens from "supertokens-node";

async function getUserInfo() {
let usersInfo = await supertokens.listUsersByAccountInfo("public", {
email: "test@example.com"
});

/**
*
* userInfo contains the following info:
* - emails
* - id
* - timeJoined
* - tenantIds
* - phone numbers
* - third party login info
* - all the login methods associated with this user.
* - information about if the user's email is verified or not.
*
*/
}
Multi Tenancy

Notice that the first argument of the above function is "public". This is the default tenantId which means that SuperTokens will return information about the user whose email is "test@example.com" in the "public" tenant.

If you are using our multi-tenancy feature, you can pass in a different tenantId to get information about a user in a different tenant.

Fetching information using the user's ID#

Retrieve the user's ID by calling:

  • the getUser function for NodeJS
  • the GetUserById function for GoLang
  • the get_user_by_id function for Python

Refer to the code snippets below for example usage:

import express from "express";
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { SessionRequest } from 'supertokens-node/framework/express';
import supertokens from "supertokens-node";

let app = express();
app.get("/get-user-info", verifySession(), async (req: SessionRequest, res) => {
let userId = req.session!.getUserId();

let userInfo = await supertokens.getUser(userId)

/**
*
* userInfo contains the following info:
* - emails
* - id
* - timeJoined
* - tenantIds
* - phone numbers
* - third party login info
* - all the login methods associated with this user.
* - information about if the user's email is verified or not.
*
*/
})

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 ThirdParty from "supertokens-node/recipe/thirdparty";
import Session from "supertokens-node/recipe/session";

SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
supertokens: {
connectionURI: "...",
},
recipeList: [
ThirdParty.init({
override: {
functions: (originalImplementation) => {
return {
...originalImplementation,
// override the thirdparty sign in / up API
signInUp: async function (input) {
// TODO: Some pre sign in / up logic

let response = await originalImplementation.signInUp(input);

if (response.status === "OK") {
// This is the response from the OAuth tokens provided by the third party provider
let accessToken = response.oAuthTokens["access_token"];
// other tokens like the refresh_token or id_token are also
// available in the oAuthTokens object.

// This gives the user's info as returned by the provider's user profile endpoint.
let firstName = response.rawUserInfoFromProvider.fromUserInfoAPI!["first_name"];

// This gives the user's info from the returned ID token
// if the provider gave us an ID token
let lastName = response.rawUserInfoFromProvider.fromUserInfoAPI!["last_name"];
}

return response;
}
}
}
}
}),
Session.init({ /* ... */ })
]
});