Fetching sessions for a user
Given a user ID, we can fetch all sessions that are currently active for that user using the getAllSessionHandlesForUser
function:
- NodeJS
- GoLang
- Python
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
import Session from "supertokens-node/recipe/session";
async function getSessions() {
let userId = "someUserId" // fetch somehow
// sessionHandles is string[]
let sessionHandles = await Session.getAllSessionHandlesForUser(userId);
sessionHandles.forEach((handle) => {
/* we can do the following with the handle:
* - revoke this session
* - change access token payload or session data
* - fetch access token payload or session data
*/
})
}
import (
"fmt"
"github.com/supertokens/supertokens-golang/recipe/session"
)
func main() {
// sessionHandles is string[]
sessionHandles, err := session.GetAllSessionHandlesForUser("someUserId")
if err != nil {
// TODO: handle error
return
}
for _, currSessionHandle := range sessionHandles {
/* we can do the following with the currSessionHandle:
* - revoke this session
* - change access token payload or session data
* - fetch access token payload or session data
*/
fmt.Println(currSessionHandle)
}
}
- Asyncio
- Syncio
from supertokens_python.recipe.session.asyncio import get_all_session_handles_for_user
async def some_func():
# session_handles is List[string]
session_handles = await get_all_session_handles_for_user("someUserId")
for _ in session_handles:
pass # TODO
#
# we can do the following with the session_handle:
# - revoke this session
# - change JWT payload or session data
# - fetch JWT payload or session data
#
from supertokens_python.recipe.session.syncio import get_all_session_handles_for_user
# session_handles is List[string]
session_handles = get_all_session_handles_for_user("someUserId")
for session_handle in session_handles:
pass # TODO
#
# we can do the following with the session_handle:
# - revoke this session
# - change JWT payload or session data
# - fetch JWT payload or session data
#
Multi Tenancy
By default, getAllSessionHandlesForUser will fetch all the sessionHandles for the user across all the tenants. If you want to delete the sessions for a user in a specific tenant, you can pass the tenant ID as a parameter to the function call.