iedan
iedan
BABetter Auth
Created by iedan on 4/28/2025 in #help
Is it possible to get the user id from the cookie without having to verify to user and hit the DB?
Right now I have a call to the database that will check if the user with the id has access to the resource being requested. Because of this I need to get the session which can take ~200ms uncached. I don't really need to verify the user is valid before I get the resource and it would speed things up quite a bit if I could just do that at the same time as I am getting the resource. Is it possible to just get the userId from the cookie / apiToken without having to hit the DB first? Right now the code looks something like:
const session = await auth.api.getSession(...);

const resource = await getResource(session.user.id);
const session = await auth.api.getSession(...);

const resource = await getResource(session.user.id);
but ideally I could do something like:
// gets the userId from the session cookie without hitting the DB
const userId = await auth.api.getUserId();

// do this in parallel
const session = auth.api.getSession(...);
const resource = getResource(userId);

if (!session) error(401);
// gets the userId from the session cookie without hitting the DB
const userId = await auth.api.getUserId();

// do this in parallel
const session = auth.api.getSession(...);
const resource = getResource(userId);

if (!session) error(401);
6 replies