Seems like convex auth isn't setting user immediately
for some reason so if I redirect to a page that checks if there is a valid user if there isn't it redirects me back to sign in so now immediately I redirect to "/dashboard" it redirects me back to "/sign-in"
My code for getting currently logged in user
The Component
My code for getting currently logged in user
import { query } from './_generated/server';
import { getAuthUserId } from '@convex-dev/auth/server';
export const currentUser = query({
args: {},
handler: async (ctx) => {
const userId = await getAuthUserId(ctx);
if (userId === null) {
return null;
}
return await ctx.db.get(userId);
},
});import { query } from './_generated/server';
import { getAuthUserId } from '@convex-dev/auth/server';
export const currentUser = query({
args: {},
handler: async (ctx) => {
const userId = await getAuthUserId(ctx);
if (userId === null) {
return null;
}
return await ctx.db.get(userId);
},
});The Component
import { api } from '#convex/_generated/api';
import { convexQuery } from '@convex-dev/react-query';
import { useQuery } from '@tanstack/react-query';
import { createFileRoute, Outlet } from '@tanstack/react-router';
import { useConvexAuth } from 'convex/react';
import { useEffect } from 'react';
export const Route = createFileRoute('/_auth')({
component: AuthLayout,
});
function AuthLayout() {
const { isAuthenticated, isLoading } = useConvexAuth();
const navigate = Route.useNavigate();
const { data, isPending, error } = useQuery(
convexQuery(api.users.currentUser, {}),
);
useEffect(() => {
if (!isPending && !error) {
if (data === null) {
navigate({ to: '/sign-in' });
} else if (!data.isOnboardingComplete) {
navigate({ to: '/onboarding' });
}
}
}, [isPending, error, data, navigate]);
useEffect(() => {
if (!isLoading && !isAuthenticated) {
navigate({
to: '/sign-in',
});
}
}, [isAuthenticated, isLoading, navigate]);
return (
<div>
<Outlet />
</div>
);
}import { api } from '#convex/_generated/api';
import { convexQuery } from '@convex-dev/react-query';
import { useQuery } from '@tanstack/react-query';
import { createFileRoute, Outlet } from '@tanstack/react-router';
import { useConvexAuth } from 'convex/react';
import { useEffect } from 'react';
export const Route = createFileRoute('/_auth')({
component: AuthLayout,
});
function AuthLayout() {
const { isAuthenticated, isLoading } = useConvexAuth();
const navigate = Route.useNavigate();
const { data, isPending, error } = useQuery(
convexQuery(api.users.currentUser, {}),
);
useEffect(() => {
if (!isPending && !error) {
if (data === null) {
navigate({ to: '/sign-in' });
} else if (!data.isOnboardingComplete) {
navigate({ to: '/onboarding' });
}
}
}, [isPending, error, data, navigate]);
useEffect(() => {
if (!isLoading && !isAuthenticated) {
navigate({
to: '/sign-in',
});
}
}, [isAuthenticated, isLoading, navigate]);
return (
<div>
<Outlet />
</div>
);
}