Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auth config to the object returned from useAuth #292

Merged
merged 1 commit into from
Oct 27, 2023
Merged
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
40 changes: 40 additions & 0 deletions packages/react/spec/auth/useAuth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,44 @@ describe("useAuth", () => {
rerender();
}).toThrowErrorMatchingInlineSnapshot(`"api client does not have a User model"`);
});

test("returns the correct auth config when signed out", () => {
const { result, rerender } = renderHook(() => useAuth(), {
wrapper: MockClientWrapper(fullAuthApi, undefined, {
signInPath: "/foo-sign-in",
signOutActionApiIdentifier: "newSignOut",
redirectOnSuccessfulSignInPath: "/bar-signed-in",
}),
});

expectMockSignedOutUser();
rerender();

expect(result.current.configuration).toEqual({
signInPath: "/foo-sign-in",
signOutActionApiIdentifier: "newSignOut",
redirectOnSuccessfulSignInPath: "/bar-signed-in",
});
expect(result.current.isSignedIn).toBe(false);
});

test("returns the correct auth config when signed in", () => {
const { result, rerender } = renderHook(() => useAuth(), {
wrapper: MockClientWrapper(fullAuthApi, undefined, {
signInPath: "/foo-sign-in",
signOutActionApiIdentifier: "newSignOut",
redirectOnSuccessfulSignInPath: "/bar-signed-in",
}),
});

expectMockSignedInUser();
rerender();

expect(result.current.configuration).toEqual({
signInPath: "/foo-sign-in",
signOutActionApiIdentifier: "newSignOut",
redirectOnSuccessfulSignInPath: "/bar-signed-in",
});
expect(result.current.isSignedIn).toBe(true);
});
});
12 changes: 11 additions & 1 deletion packages/react/src/auth/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { DefaultSelection, GadgetRecord, Select } from "@gadgetinc/api-client-core";
import { useContext } from "react";
import type { OptionsType, ReadOperationOptions } from "src/utils";
import type { GadgetAuthConfiguration } from "../GadgetProvider.js";
import { GadgetConfigurationContext } from "../GadgetProvider.js";
import type { ClientWithSessionAndUserManagers, GadgetSession, GadgetUser } from "./useSession.js";
import { useSession } from "./useSession.js";
import { useUser } from "./useUser.js";
Expand Down Expand Up @@ -47,8 +50,15 @@ export const useAuth = <
>
| GadgetUser;
isSignedIn: boolean;
configuration: GadgetAuthConfiguration;
} => {
const session = useSession(client);
const user = useUser(client);
return { session, user, isSignedIn: !!user };
const context = useContext(GadgetConfigurationContext);

if (!context) {
throw new Error("useAuth must be used within a GadgetProvider");
}

return { session, user, isSignedIn: !!user, configuration: context.auth };
};