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-07-17 07:30:44 +00:00
|
|
|
import githubBlackImage from "/public/logos/github-black.png";
|
|
|
|
import githubWhiteImage from "/public/logos/github-white.png";
|
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) => {
|
|
|
|
const { handleSignIn, clientId } = props;
|
|
|
|
// 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-07-17 07:30:44 +00:00
|
|
|
const { theme } = 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-07-17 07:30:44 +00:00
|
|
|
<div className="w-full flex justify-center items-center">
|
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
|
|
|
|
className={`flex w-full items-center justify-center gap-2 hover:bg-onboarding-background-300 rounded border border-onboarding-border-200 p-2 text-sm font-medium text-custom-text-100 duration-300 h-[46px]`}
|
|
|
|
>
|
2023-07-17 07:30:44 +00:00
|
|
|
<Image
|
2023-07-18 09:30:06 +00:00
|
|
|
src={theme === "dark" ? githubWhiteImage : githubBlackImage}
|
2023-07-17 07:30:44 +00:00
|
|
|
height={20}
|
|
|
|
width={20}
|
|
|
|
alt="GitHub Logo"
|
|
|
|
/>
|
2023-11-20 14:01:19 +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
|
|
|
);
|
|
|
|
};
|