Skip to content
Open
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
47 changes: 46 additions & 1 deletion npm-packages/convex/src/react/auth_helpers.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
/**
* @vitest-environment custom-vitest-environment.ts
*/
import { test } from "vitest";
import { render, screen } from "@testing-library/react";
import React from "react";
import { describe, expect, test } from "vitest";
import { Authenticated, AuthLoading, Unauthenticated } from "./auth_helpers.js";
import { ConvexReactClient } from "./client.js";
import { ConvexProviderWithAuth } from "./index.js";

function mockUseAuth(isLoading = true, isAuthenticated = false) {
return () => {
return {
isAuthenticated: isAuthenticated,
isLoading: isLoading,
fetchAccessToken: async () => null,
};
};
}

test("Helpers are valid children", () => {
const _element = (
Expand Down Expand Up @@ -33,3 +46,35 @@ test("Helpers can take many children", () => {
</div>
);
});

describe("<Unauthenticated />", () => {
test("renders children when loading and prop loadingEqualsUnauthenticated is true", () => {
const convex = new ConvexReactClient("https://127.0.0.1:3001");

const { unmount } = render(
<ConvexProviderWithAuth client={convex} useAuth={mockUseAuth(true)}>
<Unauthenticated loadingEqualsUnauthenticated>
<div>Yay</div>
</Unauthenticated>
</ConvexProviderWithAuth>,
);

expect(screen.getByText("Yay")).toBeTruthy();
unmount();
});

test("shows no children when loading", () => {
const convex = new ConvexReactClient("https://127.0.0.1:3001");

const { unmount } = render(
<ConvexProviderWithAuth client={convex} useAuth={mockUseAuth(true)}>
<Unauthenticated>
<div>Yay</div>
</Unauthenticated>
</ConvexProviderWithAuth>,
);

expect(screen.queryByText("Yay")).toBeNull();
unmount();
});
});
15 changes: 11 additions & 4 deletions npm-packages/convex/src/react/auth_helpers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";
import { ReactNode } from "react";
import React, { ReactNode } from "react";
import { useConvexAuth } from "./ConvexAuthState.js";

/**
Expand All @@ -17,12 +16,20 @@ export function Authenticated({ children }: { children: ReactNode }) {

/**
* Renders children if the client is using authentication but is not authenticated.
* If `loadingEqualsUnauthenticated` is `true`, also renders children while the
* client is authenticating.
*
* @public
*/
export function Unauthenticated({ children }: { children: ReactNode }) {
export function Unauthenticated({
children,
loadingEqualsUnauthenticated = false,
}: {
children: ReactNode;
loadingEqualsUnauthenticated?: boolean;
}) {
const { isLoading, isAuthenticated } = useConvexAuth();
if (isLoading || isAuthenticated) {
if ((isLoading && !loadingEqualsUnauthenticated) || isAuthenticated) {
return null;
}
return <>{children}</>;
Expand Down