HonoH
Hono12mo ago
devve2kcc

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})


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;
};
Was this page helpful?