Skip to content

Commit

Permalink
Improve auth handling
Browse files Browse the repository at this point in the history
  • Loading branch information
amanape committed Nov 22, 2024
1 parent 5e2a9a4 commit f0135ec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
8 changes: 7 additions & 1 deletion frontend/src/api/open-hands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,19 @@ class OpenHands {
static async getGitHubAccessToken(
code: string,
): Promise<GitHubAccessTokenResponse> {
return request(`/api/github/callback`, {
const response = await fetch("/api/github/callback", {
method: "POST",
body: JSON.stringify({ code }),
headers: {
"Content-Type": "application/json",
},
});

if (!response.ok) {
throw new Error("Failed to get GitHub access token");
}

return response.json();
}

/**
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/routes/_oh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ export default function MainApp() {

const config = useConfig();
const user = useGitHubUser();
const { data: isAuthed, isFetched } = useIsAuthed();
const {
data: isAuthed,
isFetched,
isFetching: isFetchingAuth,
} = useIsAuthed();
const aiConfigOptions = useAIConfigOptions();

const gitHubAuthUrl = useGitHubAuthUrl({
Expand Down Expand Up @@ -251,7 +255,7 @@ export default function MainApp() {
/>
</ModalBackdrop>
)}
{!isAuthed && config.data?.APP_MODE === "saas" && (
{!isFetchingAuth && !isAuthed && config.data?.APP_MODE === "saas" && (
<WaitlistModal ghToken={gitHubToken} githubAuthUrl={gitHubAuthUrl} />
)}
{consentFormIsOpen && (
Expand Down
42 changes: 21 additions & 21 deletions frontend/src/routes/oauth.github.callback.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import {
ClientLoaderFunctionArgs,
json,
redirect,
useLoaderData,
} from "@remix-run/react";
import { useNavigate, useSearchParams } from "@remix-run/react";
import { useQuery } from "@tanstack/react-query";
import React from "react";
import OpenHands from "#/api/open-hands";
import { useAuth } from "#/context/auth-context";

export const clientLoader = async ({ request }: ClientLoaderFunctionArgs) => {
const url = new URL(request.url);
const code = url.searchParams.get("code");

if (code) {
const { access_token: accessToken } =
await OpenHands.getGitHubAccessToken(code);
function OAuthGitHubCallback() {
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const { setGitHubToken } = useAuth();

localStorage.setItem("ghToken", accessToken);
const code = searchParams.get("code");

return redirect("/");
}
const { data, isSuccess, error } = useQuery({
queryKey: ["access_token", code],
queryFn: () => OpenHands.getGitHubAccessToken(code!),
enabled: !!code,
});

return json({ error: "No code provided" }, { status: 400 });
};
React.useEffect(() => {
if (isSuccess) {
setGitHubToken(data.access_token);
navigate("/");
}
}, [isSuccess]);

function OAuthGitHubCallback() {
const { error } = useLoaderData<typeof clientLoader>();
if (error) {
return (
<div>
<h1>Error</h1>
<p>{error}</p>
<p>{error.message}</p>
</div>
);
}
Expand Down

0 comments on commit f0135ec

Please sign in to comment.