rustclan
rustclan2y ago

calling api endpoints from within a trpc api endpoint?

Hey all. I have a series of api endpoints which may have to be reused across other api endpoints. I was wondering if this is possible?
getAccessToken: protectedProcedure.query(async ({ ctx }) => {
if (!ctx.session?.user.id) return null;
const data = await prisma?.account.findFirst({
where: {
userId: ctx.session.user.id,
},
});
return data?.access_token;
}),
checkGuildAccess: protectedProcedure
.input(z.object({ guildId: z.string() }))
.query(async (ctx) => {
const accessToken = trpc.auth.getAccessToken.useQuery();
console.log(accessToken);
if (!accessToken) return null;
return accessToken;
}),
});
getAccessToken: protectedProcedure.query(async ({ ctx }) => {
if (!ctx.session?.user.id) return null;
const data = await prisma?.account.findFirst({
where: {
userId: ctx.session.user.id,
},
});
return data?.access_token;
}),
checkGuildAccess: protectedProcedure
.input(z.object({ guildId: z.string() }))
.query(async (ctx) => {
const accessToken = trpc.auth.getAccessToken.useQuery();
console.log(accessToken);
if (!accessToken) return null;
return accessToken;
}),
});
As you can see, my checkGuildAccess needs access to the users access token. So I call the getAccessToken endpoint from within the checkGuildAccess endpoint. Is this valid? I am getting an error.
❌ tRPC failed on auth.checkGuildAccess: TRPCError: Cannot read properties of null (reading 'useContext')
❌ tRPC failed on auth.checkGuildAccess: TRPCError: Cannot read properties of null (reading 'useContext')
1 Reply
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View