Skip to content

Commit

Permalink
moved oauth url stuff to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jmderby committed Oct 17, 2024
1 parent 0ff9d02 commit d995b7f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 51 deletions.
22 changes: 22 additions & 0 deletions packages/client/ui/react-ui/src/hooks/useAuthSignIn.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { OAuthProvider } from "@/types/auth";
import type { UseSignInData } from "@farcaster/auth-kit";

export function useAuthSignIn() {
return {
onEmailSignIn,
onConfirmEmailOtp,
onFarcasterSignIn,
getOAuthUrl,
};
}

Expand Down Expand Up @@ -102,3 +104,23 @@ async function onFarcasterSignIn(data: UseSignInData, options: { baseUrl: string
throw new Error("Error signing in via farcaster " + err);
}
}
async function getOAuthUrl(provider: OAuthProvider, options: { baseUrl: string; apiKey: string }) {
try {
const queryParams = new URLSearchParams({
baseUrl: options.baseUrl,
apiKey: options.apiKey,
signinAuthenticationMethod: provider,
});
const response = await fetch(`${options.baseUrl}api/2024-09-26/session/sdk/auth/oauth?${queryParams}`);

if (!response.ok) {
throw new Error("Failed to get OAuth URL. Please try again or contact support.");
}

const data = (await response.json()) as { oauthUrl: string };
return data.oauthUrl;
} catch (error) {
console.error("Error fetching OAuth URL:", error);
throw new Error("Failed to get OAuth URL. Please try again or contact support.");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { OAuthProvider } from "@/types/auth";
import { generateOAuthUrl } from "@/utils/generateOAuthUrl";
import { ChildWindow, PopupWindow } from "@crossmint/client-sdk-window";
import type { AuthMaterial } from "@crossmint/common-sdk-auth";
import { useEffect, useRef, useState } from "react";
import { z } from "zod";
import { useAuthSignIn } from "./useAuthSignIn";

export const useOAuthWindowListener = (
provider: OAuthProvider,
Expand All @@ -13,6 +13,7 @@ export const useOAuthWindowListener = (
fetchAuthMaterial: (refreshToken: string) => Promise<AuthMaterial>;
}
) => {
const { getOAuthUrl } = useAuthSignIn();
const [isLoading, setIsLoading] = useState(false);
const childRef = useRef<ChildWindow<IncomingEvents, OutgoingEvents> | null>(null);
const popupRef = useRef<PopupWindow<IncomingEvents, OutgoingEvents> | null>(null);
Expand Down Expand Up @@ -44,7 +45,8 @@ export const useOAuthWindowListener = (
popupWindowWidth = 600;
}

const popup = await PopupWindow.init(await generateOAuthUrl(provider, options.apiKey, options.baseUrl), {
const oauthUrl = await getOAuthUrl(provider, { apiKey: options.apiKey, baseUrl: options.baseUrl });
const popup = await PopupWindow.init(oauthUrl, {
awaitToLoad: false,
crossOrigin: true,
width: popupWindowWidth,
Expand Down
4 changes: 2 additions & 2 deletions packages/client/ui/react-ui/src/icons/poweredByLeaf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const PoweredByLeaf = ({ color, size = "16" }: { color: string; size?: st
maskUnits="userSpaceOnUse"
x="0"
y="0"
width={size}
height={size}
width="16"
height="16"
>
<mask id="path-1-inside-1_794_7000" fill="white">
<path
Expand Down
47 changes: 0 additions & 47 deletions packages/client/ui/react-ui/src/utils/generateOAuthUrl.ts

This file was deleted.

0 comments on commit d995b7f

Please sign in to comment.