Skip to content

Commit

Permalink
made changes to correspond to recent backend changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmderby committed Oct 23, 2024
1 parent 61e7754 commit 7f850ee
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ export function EmailOTPInput({
const handleOnSubmit = async () => {
setLoading(true);
try {
const oneTimeSecret = await onConfirmEmailOtp(otpEmailData?.email ?? "", otpEmailData?.state ?? "", token, {
baseUrl,
apiKey,
});
const oneTimeSecret = await onConfirmEmailOtp(
otpEmailData?.email ?? "",
otpEmailData?.emailId ?? "",
token,
{
baseUrl,
apiKey,
}
);

await fetchAuthMaterial(oneTimeSecret as string);
setDialogOpen(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,9 @@ export function EmailSignIn({ setOtpEmailData }: { setOtpEmailData: (data: OtpEm

try {
const trimmedEmailInput = emailInput.trim().toLowerCase();
const createSignInTokenRes = (await onEmailSignIn(trimmedEmailInput, { baseUrl, apiKey })) as {
user: {
email: string;
emailId: string;
userId: string;
};
};
const emailSignInRes = (await onEmailSignIn(trimmedEmailInput, { baseUrl, apiKey })) as { emailId: string };

setOtpEmailData({ email: createSignInTokenRes.user.email, state: createSignInTokenRes.user.emailId });
setOtpEmailData({ email: trimmedEmailInput, emailId: emailSignInRes.emailId });
setStep("otp");
} catch (_e: unknown) {
setIsLoading(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { useAuthForm } from "@/providers/auth/AuthFormProvider";
import { classNames } from "@/utils/classNames";

export function GoogleSignIn({ ...props }: ButtonHTMLAttributes<HTMLButtonElement>) {
const { step, apiKey, baseUrl, appearance, isLoadingOauthUrl, fetchAuthMaterial } = useAuthForm();
const { step, apiKey, baseUrl, appearance, isLoadingOauthUrlMap, fetchAuthMaterial } = useAuthForm();
const { createPopupAndSetupListeners, isLoading: isLoadingOAuthWindow } = useOAuthWindowListener("google", {
apiKey,
baseUrl,
fetchAuthMaterial,
});
const isLoading = isLoadingOauthUrl || isLoadingOAuthWindow;
const isLoading = isLoadingOauthUrlMap || isLoadingOAuthWindow;

if (step !== "initial") {
return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/client/ui/react-ui/src/hooks/useAuthSignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function onEmailSignIn(email: string, options: { baseUrl: string; apiKey:

async function onConfirmEmailOtp(
email: string,
state: string,
emailId: string,
token: string,
options: { baseUrl: string; apiKey: string }
) {
Expand All @@ -47,7 +47,7 @@ async function onConfirmEmailOtp(
apiKey: options.apiKey,
token,
locale: "en",
state,
state: emailId,
callbackUrl: `${options.baseUrl}api/2024-09-26/session/sdk/auth/we-dont-actually-use-this-anymore`,
});
const response = await fetch(`${options.baseUrl}api/2024-09-26/session/sdk/auth/authenticate?${queryParams}`, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useOAuthWindowListener = (
fetchAuthMaterial: (refreshToken: string) => Promise<AuthMaterialWithUser>;
}
) => {
const { oauthUrl } = useAuthForm();
const { oauthUrlMap } = useAuthForm();
const [isLoading, setIsLoading] = useState(false);
const childRef = useRef<ChildWindow<IncomingEvents, OutgoingEvents> | null>(null);

Expand All @@ -37,7 +37,7 @@ export const useOAuthWindowListener = (
throw new Error("Child window not initialized");
}
setIsLoading(true);
const popup = await PopupWindow.init(oauthUrl[provider], {
const popup = await PopupWindow.init(oauthUrlMap[provider], {
awaitToLoad: false,
crossOrigin: true,
width: 400,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { LoginMethod } from "..";

// Mock component to test the AuthFormProvider
function TestComponent() {
const { step, apiKey, baseUrl, loginMethods, setStep, setDialogOpen, oauthUrl, isLoadingOauthUrl } = useAuthForm();
const { step, apiKey, baseUrl, loginMethods, setStep, setDialogOpen, oauthUrlMap, isLoadingOauthUrlMap } =
useAuthForm();

return (
<div>
Expand All @@ -19,8 +20,8 @@ function TestComponent() {
<button onClick={() => setDialogOpen(true)} data-testid="set-dialog-open">
Open Dialog
</button>
<div data-testid="oauth-url">{JSON.stringify(oauthUrl)}</div>
<div data-testid="is-loading-oauth-url">{isLoadingOauthUrl.toString()}</div>
<div data-testid="oauth-url">{JSON.stringify(oauthUrlMap)}</div>
<div data-testid="is-loading-oauth-url">{isLoadingOauthUrlMap.toString()}</div>
</div>
);
}
Expand Down
22 changes: 11 additions & 11 deletions packages/client/ui/react-ui/src/providers/auth/AuthFormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ interface AuthFormContextType {
fetchAuthMaterial: (refreshToken: string) => Promise<AuthMaterialWithUser>;
appearance?: UIConfig;
loginMethods: LoginMethod[];
oauthUrl: OAuthUrlMap;
isLoadingOauthUrl: boolean;
oauthUrlMap: OAuthUrlMap;
isLoadingOauthUrlMap: boolean;
setStep: (step: AuthStep) => void;
setDialogOpen: (open: boolean) => void;
}
Expand Down Expand Up @@ -46,30 +46,30 @@ export const AuthFormProvider = ({
initialState,
}: { children: ReactNode; initialState: ContextInitialStateProps }) => {
const [step, setStep] = useState<AuthStep>("initial");
const [oauthUrl, setOauthUrl] = useState<OAuthUrlMap>(initialOAuthUrlMap);
const [isLoadingOauthUrl, setIsLoadingOauthUrl] = useState(true);
const [oauthUrlMap, setOauthUrlMap] = useState<OAuthUrlMap>(initialOAuthUrlMap);
const [isLoadingOauthUrlMap, setIsLoadingOauthUrlMap] = useState(true);

const { loginMethods, apiKey, baseUrl } = initialState;

useEffect(() => {
const preFetchAndSetOauthUrl = async () => {
setIsLoadingOauthUrl(true);
setIsLoadingOauthUrlMap(true);
try {
const oauthProviders = loginMethods.filter(
(method): method is OAuthProvider => method in initialOAuthUrlMap
);

const OAuthPromiseList = oauthProviders.map(async (provider) => {
const oauthPromiseList = oauthProviders.map(async (provider) => {
const url = await getOAuthUrl(provider, { apiKey, baseUrl });
return { [provider]: url };
});

const oauthUrlMap = Object.assign({}, ...(await Promise.all(OAuthPromiseList)));
setOauthUrl(oauthUrlMap);
const oauthUrlMap = Object.assign({}, ...(await Promise.all(oauthPromiseList)));
setOauthUrlMap(oauthUrlMap);
} catch (error) {
console.error("Error fetching OAuth URLs:", error);
} finally {
setIsLoadingOauthUrl(false);
setIsLoadingOauthUrlMap(false);
}
};
preFetchAndSetOauthUrl();
Expand All @@ -82,8 +82,8 @@ export const AuthFormProvider = ({
fetchAuthMaterial: initialState.fetchAuthMaterial,
appearance: initialState.appearance,
loginMethods,
oauthUrl,
isLoadingOauthUrl,
oauthUrlMap,
isLoadingOauthUrlMap,
setDialogOpen: initialState.setDialogOpen ?? (() => {}),
setStep,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/client/ui/react-ui/src/types/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type OtpEmailPayload = {
email: string;
state: string;
emailId: string;
};

0 comments on commit 7f850ee

Please sign in to comment.