Problem passing jwt token by rpc

hi guys, i have an endpoint localhost:5555/api/summary, that works fine testing on any app like postman, passing an Bearer token, but do not work when i use hono client passing the bearer token, always says that the token is not passed
const app = new Hono().get("/", async (c) => {
const user = c.get("jwtPayload");

if (!user) {
return c.json({ error: "Unauthorized" }, 401);
}

return c.json({success: true})
const app = new Hono().get("/", async (c) => {
const user = c.get("jwtPayload");

if (!user) {
return c.json({ error: "Unauthorized" }, 401);
}

return c.json({success: true})
export const useGetSummary = () => {
const { user } = useUser()
const query = useQuery({
queryKey: ["summary"],
queryFn: async () => {
const token = user?.token
console.log(token); // the token exists, because is primted on console.

const response = await client.api.summary.$get({
headers: {
Authorization: `Bearer ${token}` //im passing there the bearer token
}
});

if (!response.ok) {
throw new Error("Failed to fetch transactions!");
}

const { data } = await response.json();
return {
...data,
incomeAmount: convertAmountFromMiliunits(data.incomeAmount),
expensesAmount: convertAmountFromMiliunits(data.expensesAmount),
remainingAmount: convertAmountFromMiliunits(data.remainingAmount),
categories: data.categories.map((category) => ({
...category,
value: convertAmountFromMiliunits(category.value),
})),
days: data.days.map((day) => ({
...day,
income: convertAmountFromMiliunits(day.income),
expenses: convertAmountFromMiliunits(day.expenses),
})),
};
},
});
return query;
};
export const useGetSummary = () => {
const { user } = useUser()
const query = useQuery({
queryKey: ["summary"],
queryFn: async () => {
const token = user?.token
console.log(token); // the token exists, because is primted on console.

const response = await client.api.summary.$get({
headers: {
Authorization: `Bearer ${token}` //im passing there the bearer token
}
});

if (!response.ok) {
throw new Error("Failed to fetch transactions!");
}

const { data } = await response.json();
return {
...data,
incomeAmount: convertAmountFromMiliunits(data.incomeAmount),
expensesAmount: convertAmountFromMiliunits(data.expensesAmount),
remainingAmount: convertAmountFromMiliunits(data.remainingAmount),
categories: data.categories.map((category) => ({
...category,
value: convertAmountFromMiliunits(category.value),
})),
days: data.days.map((day) => ({
...day,
income: convertAmountFromMiliunits(day.income),
expenses: convertAmountFromMiliunits(day.expenses),
})),
};
},
});
return query;
};
4 Replies
devve2kcc
devve2kccOP3w ago
const response = await client.api.summary.$get(undefined, {
headers: {
Authorization: `Bearer ${token ?? ''}`
}
});
const response = await client.api.summary.$get(undefined, {
headers: {
Authorization: `Bearer ${token ?? ''}`
}
});
i do not belive, 5h trywing to find the issue, and now i find in a github issue, is just put undefined before passing headers...
ambergristle
ambergristle3w ago
hey @devve2kcc! could you share a link to the github issue you found? but yeah, though its not mentioned explicitly in the docs, the first arg of all the fetch functions is for request data (including params + queries)
devve2kcc
devve2kccOP3w ago
GitHub
Not able to pass the bearer token as a headers not working · Issue ...
What version of Hono are you using? ^4.6.12 What runtime/platform is your app running on? (with version if possible) Node What steps can reproduce the bug? import { client } from '@/lib/hono...
ambergristle
ambergristle3w ago
ah, i see. i looked for this and didn't see it: https://hono.dev/docs/guides/rpc#headers
RPC - Hono
Web framework built on Web Standards for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.

Did you find this page helpful?