2023-01-26 18:12:20 +00:00
|
|
|
import { useEffect, useState, FC } from "react";
|
|
|
|
import Link from "next/link";
|
|
|
|
import Image from "next/image";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
// images
|
2023-03-21 07:16:12 +00:00
|
|
|
import githubImage from "/public/logos/github-black.png";
|
2023-01-26 18:12:20 +00:00
|
|
|
|
|
|
|
const { NEXT_PUBLIC_GITHUB_ID } = process.env;
|
|
|
|
|
|
|
|
export interface GithubLoginButtonProps {
|
|
|
|
handleSignIn: React.Dispatch<string>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const GithubLoginButton: FC<GithubLoginButtonProps> = (props) => {
|
|
|
|
const { handleSignIn } = props;
|
|
|
|
// router
|
|
|
|
const {
|
|
|
|
query: { code },
|
|
|
|
} = useRouter();
|
|
|
|
// states
|
|
|
|
const [loginCallBackURL, setLoginCallBackURL] = useState(undefined);
|
2023-06-09 19:56:38 +00:00
|
|
|
const [gitCode, setGitCode] = useState<null | string>(null);
|
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(() => {
|
|
|
|
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-06-13 09:07:25 +00:00
|
|
|
<div className="w-full flex justify-center items-center px-[3px]">
|
2023-03-21 07:16:12 +00:00
|
|
|
<Link
|
|
|
|
href={`https://github.com/login/oauth/authorize?client_id=${NEXT_PUBLIC_GITHUB_ID}&redirect_uri=${loginCallBackURL}&scope=read:user,user:email`}
|
|
|
|
>
|
2023-06-13 09:07:25 +00:00
|
|
|
<button className="flex w-full items-center justify-center gap-3 rounded border border-brand-base p-2 text-sm font-medium text-brand-secondary duration-300 hover:bg-brand-surface-2">
|
|
|
|
<Image src={githubImage} height={20} width={20} color="#000" alt="GitHub Logo" />
|
2023-03-21 07:16:12 +00:00
|
|
|
<span>Sign In with Github</span>
|
|
|
|
</button>
|
|
|
|
</Link>
|
|
|
|
</div>
|
2023-01-26 18:12:20 +00:00
|
|
|
);
|
|
|
|
};
|