-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpage.tsx
More file actions
102 lines (84 loc) · 3 KB
/
page.tsx
File metadata and controls
102 lines (84 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { Flex, Paper, Progress, Stack } from "@mantine/core";
import { useCallback } from "react";
import { useEffect } from "react";
import { useNavigate, useSearchParams } from "react-router";
import { useLoginWithGithubMutation } from "../../../api/auth.api";
import { Logo } from "../../../components/logo";
import { setAuth } from "../../../providers/auth.provider";
import { showErrorNotification } from "../../../providers/notification.provider";
import { isGithubInstallSuccessCallback } from "../../../providers/github.provider";
import { forwardQueryParameters } from "../../../providers/url.provider";
const getRedirectUrl = (encodedUrl?: string) => {
if (isGithubInstallSuccessCallback(new URL(window.location.href))) {
const redirectTo = forwardQueryParameters(
new URL("/github/install", window.location.origin),
);
return `${redirectTo.pathname}${redirectTo.search}`;
}
if (encodedUrl) return decodeURIComponent(encodedUrl);
return "/";
};
export const OAuthGithubPage = () => {
const [searchParams] = useSearchParams({
code: "",
state: "",
});
const code = searchParams.get("code");
const state = searchParams.get("state");
const stateEncodedUrl = state?.split(":::").at(0);
const csrfToken = state?.split(":::").at(-1);
const redirectUrl = getRedirectUrl(stateEncodedUrl);
const navigate = useNavigate();
document.body.style.backgroundColor = "#141517";
const handleError = useCallback(() => {
if (!csrfToken) {
showErrorNotification({
title: "Error",
message:
"Could not verify the request origin, please authenticate again.",
autoClose: false,
});
} else {
showErrorNotification({
title: "Error",
message: "Something went wrong, please try again.",
});
}
if (isGithubInstallSuccessCallback(new URL(window.location.href))) {
const installationId = searchParams.get("installation_id");
navigate(
`/login?redirectTo=${encodeURIComponent(`/github/install?installation_id=${installationId}`)}`,
);
return;
}
navigate("/");
}, [navigate, csrfToken, searchParams]);
const { data, error, isPending, mutate } = useLoginWithGithubMutation({
onSuccess: (data) => {
const accessToken = data.loginWithGithub.token.accessToken;
setAuth(accessToken);
navigate(redirectUrl);
},
onError: handleError,
});
useEffect(() => {
if (code && csrfToken) {
mutate({ input: { code, state: csrfToken } });
return;
}
handleError();
}, [code, csrfToken, redirectUrl, mutate, navigate, handleError]);
return (
<Flex mih="100vh" align="center" direction="column">
<Stack mt={120} align="center">
<Logo size={128} />
<Paper withBorder mt={40} p="lg" w={260}>
{(isPending || data) && (
<Progress color="green" size="md" value={100} animated />
)}
{error && <>Invalid code, please try again.</>}
</Paper>
</Stack>
</Flex>
);
};