2023-11-20 14:01:19 +00:00
|
|
|
// react
|
2023-01-26 18:12:20 +00:00
|
|
|
import { useEffect, useState, FC } from "react";
|
2023-11-20 14:01:19 +00:00
|
|
|
// next
|
2023-01-26 18:12:20 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
import Image from "next/image";
|
|
|
|
import { useRouter } from "next/router";
|
2023-07-17 07:30:44 +00:00
|
|
|
import { useTheme } from "next-themes";
|
2023-01-26 18:12:20 +00:00
|
|
|
// images
|
2023-11-23 08:15:00 +00:00
|
|
|
import githubLightModeImage from "/public/logos/github-black.png";
|
|
|
|
import githubDarkModeImage from "/public/logos/github-dark.svg";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
export interface GithubLoginButtonProps {
|
|
|
|
handleSignIn: React.Dispatch<string>;
|
2023-10-04 13:25:29 +00:00
|
|
|
clientId: string;
|
2023-01-26 18:12:20 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 13:25:29 +00:00
|
|
|
export const GithubLoginButton: FC<GithubLoginButtonProps> = (props) => {
|
2023-11-29 13:37:33 +00:00
|
|
|
const { handleSignIn, clientId } = props;
|
2023-10-04 13:25:29 +00:00
|
|
|
// states
|
2023-07-17 07:30:44 +00:00
|
|
|
const [loginCallBackURL, setLoginCallBackURL] = useState(undefined);
|
|
|
|
const [gitCode, setGitCode] = useState<null | string>(null);
|
2023-10-04 13:25:29 +00:00
|
|
|
// router
|
2023-01-26 18:12:20 +00:00
|
|
|
const {
|
|
|
|
query: { code },
|
|
|
|
} = useRouter();
|
2023-10-04 13:25:29 +00:00
|
|
|
// theme
|
2023-11-23 08:15:00 +00:00
|
|
|
const { resolvedTheme } = useTheme();
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-06-09 19:56:38 +00:00
|
|
|
if (code && !gitCode) {
|
|
|
|
setGitCode(code.toString());
|
2023-01-26 18:12:20 +00:00
|
|
|
handleSignIn(code.toString());
|
|
|
|
}
|
2023-06-09 19:56:38 +00:00
|
|
|
}, [code, gitCode, handleSignIn]);
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-11-08 15:01:46 +00:00
|
|
|
const origin = typeof window !== "undefined" && window.location.origin ? window.location.origin : "";
|
2023-05-30 13:44:35 +00:00
|
|
|
setLoginCallBackURL(`${origin}/` as any);
|
2023-01-26 18:12:20 +00:00
|
|
|
}, []);
|
|
|
|
return (
|
2023-12-06 08:52:59 +00:00
|
|
|
<div className="w-full">
|
2023-03-21 07:16:12 +00:00
|
|
|
<Link
|
2023-10-04 13:25:29 +00:00
|
|
|
href={`https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${loginCallBackURL}&scope=read:user,user:email`}
|
2023-03-21 07:16:12 +00:00
|
|
|
>
|
2023-11-20 14:01:19 +00:00
|
|
|
<button
|
2023-12-10 10:18:10 +00:00
|
|
|
className={`flex h-[42px] w-full items-center justify-center gap-2 rounded border px-2 text-sm font-medium text-custom-text-100 duration-300 hover:bg-onboarding-background-300 ${
|
|
|
|
resolvedTheme === "dark" ? "border-[#43484F] bg-[#2F3135]" : "border-[#D9E4FF]"
|
2023-11-23 08:15:00 +00:00
|
|
|
}`}
|
2023-11-20 14:01:19 +00:00
|
|
|
>
|
2023-07-17 07:30:44 +00:00
|
|
|
<Image
|
2023-11-23 08:15:00 +00:00
|
|
|
src={resolvedTheme === "dark" ? githubDarkModeImage : githubLightModeImage}
|
2023-07-17 07:30:44 +00:00
|
|
|
height={20}
|
|
|
|
width={20}
|
|
|
|
alt="GitHub Logo"
|
|
|
|
/>
|
2023-11-29 13:37:33 +00:00
|
|
|
<span className="text-onboarding-text-200">Sign-in with GitHub</span>
|
2023-03-21 07:16:12 +00:00
|
|
|
</button>
|
|
|
|
</Link>
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
};
|