auth.api.signInAnonymous doesn't set session cookie.

I'm trying to set some endpoints to create anonymous session if the user is offline in order to link it with an account later, but the problem is that when signing user in, it doesn't set the cookie in the client for some reason, is there a workaround for it? I'm using tRPC with Elysia. Here is the code for my procedure.

import { initTRPC, TRPCError } from "@trpc/server";
import type { Context } from "./context";
import { auth } from "./auth";
export const t = initTRPC.context<Context>().create();

export const authenticationOrAnonymousProcedure = t.procedure.use(
  async ({ ctx, next }) => {
    let session = ctx.session;
    if (!session) {
      const anon = await auth.api.signInAnonymous({
        headers: ctx.headers,
        returnHeaders: true,
      });
      session = await auth.api.getSession({
        headers: anon.headers, // Tried ctx.headers and didn't work either.
      });
    }
    return next({
      ctx: {
        ...ctx,
        headers: ctx.headers,
        session: session!,
      },
    });
  }
);
Was this page helpful?