auth.session

The auth.session namespace provides methods for managing user sessions.

The ctx.auth examples on this page assume the handler is using auth.ctx()- backed builders such as authQuery, authMutation, or authAction.

Methods

MethodSignatureReturnsDescription
invalidate(ctx, { userId, except? }){ userId, except }Invalidates all sessions for a user. Pass except as an array of session IDs to keep those active.
get(ctx, sessionId)Doc<"Session"> \| nullFetches a session document by ID.
list(ctx, { userId })Doc<"Session">[]Lists all sessions for a user.

Examples

Read the current session ID from native identity

const identity = await ctx.auth.getUserIdentity();
const sessionId = identity?.sid;

Invalidate all other sessions

This is useful for a “sign out everywhere else” feature:

const identity = await ctx.auth.getUserIdentity();
const sessionId = identity?.sid;
if (!sessionId) {
  throw new Error("Current session missing");
}

await auth.session.invalidate(ctx, {
  userId: ctx.auth.userId,
  except: [sessionId],
});

List all sessions for a user

const sessions = await auth.session.list(ctx, { userId: ctx.auth.userId });