Skip to content

Commit

Permalink
Store callback state before OAuth navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
jpellizzari committed Nov 9, 2021
1 parent ac8d583 commit dcf6c59
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion ui/components/AuthAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Message = ({ onClick, provider }) => (
{provider === GitProvider.GitHub ? (
<GithubAuthButton onClick={onClick} />
) : (
<GitlabAuthButton />
<GitlabAuthButton onClick={onClick} />
)}
</Flex>
);
Expand Down
20 changes: 7 additions & 13 deletions ui/components/GitlabAuthButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,23 @@ import { ButtonProps } from "@material-ui/core";
import * as React from "react";
import styled from "styled-components";
import { AppContext } from "../contexts/AppContext";
import useNavigation from "../hooks/navigation";
import { CallbackSessionState } from "../lib/storage";
import { PageRoute } from "../lib/types";
import { gitlabOAuthRedirectURI } from "../lib/utils";
import Button from "./Button";

type Props = { callbackState?: CallbackSessionState } & ButtonProps;
type Props = ButtonProps;

function GitlabAuthButton({ callbackState, ...props }: Props) {
const { currentPage } = useNavigation();
const { applicationsClient, storeCallbackState, navigate } =
React.useContext(AppContext);
function GitlabAuthButton({ ...props }: Props) {
const { applicationsClient, navigate } = React.useContext(AppContext);

const handleClick = () => {
const handleClick = (e) => {
if (props.onClick) {
props.onClick(e);
}
applicationsClient
.GetGitlabAuthURL({
redirectUri: gitlabOAuthRedirectURI(),
})
.then((res) => {
if (!callbackState) {
callbackState = { page: `/${currentPage}` as PageRoute, state: null };
}
storeCallbackState(callbackState);
navigate(res.url);
});
};
Expand Down
7 changes: 2 additions & 5 deletions ui/components/RepoInputWithAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
GitProvider,
ParseRepoURLResponse,
} from "../lib/api/applications/applications.pb";
import { CallbackSessionState } from "../lib/storage";
import Button from "./Button";
import Flex from "./Flex";
import GithubAuthButton from "./GithubAuthButton";
Expand All @@ -16,16 +15,14 @@ import Icon, { IconType } from "./Icon";
import Input, { InputProps } from "./Input";

type Props = InputProps & {
onAuthClick?: (provider: GitProvider) => void;
callbackState: CallbackSessionState;
onAuthClick: (provider: GitProvider) => void;
onProviderChange?: (provider: GitProvider) => void;
isAuthenticated?: boolean;
};

function RepoInputWithAuth({
onAuthClick,
onProviderChange,
callbackState,
isAuthenticated,
...props
}: Props) {
Expand Down Expand Up @@ -58,7 +55,7 @@ function RepoInputWithAuth({
}}
/>
) : (
<GitlabAuthButton callbackState={callbackState} />
<GitlabAuthButton onClick={() => onAuthClick(GitProvider.GitLab)} />
);

const renderProviderAuthButton =
Expand Down
13 changes: 2 additions & 11 deletions ui/components/__tests__/RepoInputWithAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ describe("RepoInputWithAuth", () => {
await act(async () => {
render(
withTheme(
withContext(
<RepoInputWithAuth
onAuthClick={() => null}
callbackState={null}
/>,
"/",
{}
)
withContext(<RepoInputWithAuth onAuthClick={() => null} />, "/", {})
)
);
});
Expand All @@ -57,7 +50,6 @@ describe("RepoInputWithAuth", () => {
<RepoInputWithAuth
value={url}
onAuthClick={() => null}
callbackState={null}
onProviderChange={onProviderChange}
/>,
"/",
Expand Down Expand Up @@ -91,7 +83,6 @@ describe("RepoInputWithAuth", () => {
value={url}
onAuthClick={onAuthClick}
onProviderChange={onProviderChange}
callbackState={null}
/>,
"/",
c
Expand Down Expand Up @@ -132,7 +123,7 @@ describe("RepoInputWithAuth", () => {
<RepoInputWithAuth
value={repoUrl}
onProviderChange={onProviderChange}
callbackState={null}
onAuthClick={() => null}
/>,
"/",
c
Expand Down
13 changes: 9 additions & 4 deletions ui/pages/ApplicationAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import RepoInputWithAuth from "../components/RepoInputWithAuth";
import { AppContext } from "../contexts/AppContext";
import { useAddApplication } from "../hooks/applications";
import { GitProvider } from "../lib/api/applications/applications.pb";
import { storeCallbackState } from "../lib/storage";
import { GrpcErrorCodes, PageRoute } from "../lib/types";

type Props = {
Expand Down Expand Up @@ -164,6 +165,7 @@ function AddApplication({ className }: Props) {
};

const handleAuthClick = () => {
storeCallbackState({ page: PageRoute.ApplicationAdd, state: formState });
setAuthOpen(true);
};

Expand Down Expand Up @@ -246,10 +248,6 @@ function AddApplication({ className }: Props) {
<FormElement>
<RepoInputWithAuth
isAuthenticated={!!formState.url && credentialsDetected}
callbackState={{
page: PageRoute.ApplicationAdd,
state: formState,
}}
onChange={(e) => {
setFormState({
...formState,
Expand All @@ -263,6 +261,13 @@ function AddApplication({ className }: Props) {
if (provider === GitProvider.GitHub) {
setAuthOpen(true);
}

if (provider === GitProvider.GitLab) {
storeCallbackState({
page: PageRoute.ApplicationAdd,
state: formState,
});
}
}}
required
id="url"
Expand Down

0 comments on commit dcf6c59

Please sign in to comment.