Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions apps/app/src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@

import { isSafeRedirectPath } from '@op/common/client';
import { useAuthUser } from '@op/hooks';
import { redirect, useSearchParams } from 'next/navigation';
import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect } from 'react';

import { LoginPanel } from '@/components/LoginPanel';

const LoginPageWithLayout = () => {
return <LoginPanel />;
};

const LoginPage = () => {
const user = useAuthUser();
const router = useRouter();
const searchParams = useSearchParams();
const redirectParam = searchParams.get('redirect');

useEffect(() => {
if (!user || user.isFetching || user.isPending) {
return;
}

if (user.isFetchedAfterMount && !user.data?.user) {
return;
}

if (isSafeRedirectPath(redirectParam)) {
router.replace(redirectParam);
} else {
router.replace('/');
}
}, [user, redirectParam, router]);

if (!user || user.isFetching || user.isPending) {
return null;
}

if (user.isFetchedAfterMount && !user.isFetching && !user.data?.user) {
return <LoginPageWithLayout />;
}

if (isSafeRedirectPath(redirectParam)) {
redirect(redirectParam);
return <LoginPanel />;
}

redirect('/');
return null;
};

export default LoginPage;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
taxonomyTerms,
} from '@op/db/schema';
import { randomUUID } from 'node:crypto';
import { describe, expect, it } from 'vitest';
import { afterAll, describe, expect, it } from 'vitest';

import { appRouter } from '../..';
import { TestDecisionsDataManager } from '../../../test/helpers/TestDecisionsDataManager';
Expand Down Expand Up @@ -42,11 +42,9 @@ async function seedProposalTaxonomy(
.returning({ id: taxonomies.id });

let resolvedTaxonomyId: string;
let ownsTheTaxonomy = false;

if (inserted) {
resolvedTaxonomyId = inserted.id;
ownsTheTaxonomy = true;
} else {
// Another test created it — look up the existing one
const [existing] = await db
Expand Down Expand Up @@ -82,10 +80,6 @@ async function seedProposalTaxonomy(
),
);
}
// Only delete taxonomy if this test created it
if (ownsTheTaxonomy) {
await db.delete(taxonomies).where(eq(taxonomies.id, resolvedTaxonomyId));
}
});

return { taxonomyId: resolvedTaxonomyId, termRecords };
Expand Down Expand Up @@ -278,6 +272,10 @@ describe.concurrent('getCategories permissions', () => {
});

describe.concurrent('getCategories category matching', () => {
afterAll(async () => {
await db.delete(taxonomies).where(eq(taxonomies.name, 'proposal'));
});

it('should return matched categories when config.categories and taxonomy terms exist', async ({
task,
onTestFinished,
Expand Down
Loading