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.
  • 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 getUserByEmail, getUserByPhoneNumber and getUserById functions:


import Passwordless from "supertokens-node/recipe/passwordless";

async function handler() {
const userInfo = await Passwordless.getUserByEmail({email: "test@example.com", tenantId: "public"});
}

Multi Tenancy

Notice that we pass in the "public" tenantId to the function call above. This is the default tenantID and will return the user with the given email that belongs to the public tenant. You can provide a different tenantID if required.

Fetching information using the user's phone number#

import Passwordless from "supertokens-node/recipe/passwordless";

async function handler() {
const userInfo = await Passwordless.getUserByPhoneNumber({phoneNumber: "+1234567891", tenantId: "public"});
}
Multi Tenancy

Notice that we pass in the "public" tenantId to the function call above. This is the default tenantID and will return the user with the given phone number that belongs to the public tenant. You can provide a different tenantID if required.

Fetching information using the user's ID#

Using the getUserById function#

import express from "express";
import Passwordless from "supertokens-node/recipe/passwordless";
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 Passwordless.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.