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!,
},
});
}
);
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!,
},
});
}
);