Olamide
Olamide
CCConvex Community
Created by Olamide on 10/12/2024 in #support-community
Issue with convex auth
when using convex auth and I try signing up with password when the account exists already but the email and password I use in signing up matches the one existing it just signs them in and I don't think that is the right behaviour
2 replies
CCConvex Community
Created by Olamide on 8/30/2024 in #support-community
I noticed something with convex auth
So if a user signs up with email and password and logs out if another user tries to signs up with the same exact email and password it just signs them in automatically why?
2 replies
CCConvex Community
Created by Olamide on 8/28/2024 in #support-community
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
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>
);
}
15 replies