#Get Convex auth session
3 messages · Page 1 of 1 (latest)
Hi @lament flame. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To get the Convex auth user session, you can use the getSessionId and getUserId functions. These functions are defined in your Convex queries, mutations, and actions. Here is an example of how you can use these functions:
import { Auth } from "convex/server";
import { Id } from "./_generated/dataModel";
export async function getSessionId(ctx: { auth: Auth }) {
const identity = await ctx.auth.getUserIdentity();
if (identity === null) {
return null;
}
const [, sessionId] = identity.subject.split(";");
return sessionId as Id<"sessions">;
}
export async function getUserId(ctx: { auth: Auth }) {
const identity = await ctx.auth.getUserIdentity();
if (identity === null) {
return null;
}
const [userId] = identity.subject.split(";");
return userId as Id<"users">;
}
In the above code, getSessionId and getUserId are functions that extract the session ID and user ID from the subject field of the JWT token. The subject field is populated with both the user ID and session ID in the auth.ts file.