2023-11-29 13:37:33 +00:00
|
|
|
import { observer } from "mobx-react-lite";
|
|
|
|
// mobx store
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
|
|
|
// services
|
|
|
|
import { AuthService } from "services/auth.service";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// components
|
2023-12-11 10:22:21 +00:00
|
|
|
import { GitHubSignInButton, GoogleSignInButton } from "components/account";
|
2023-11-29 13:37:33 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
handleSignInRedirection: () => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
// services
|
|
|
|
const authService = new AuthService();
|
|
|
|
|
|
|
|
export const OAuthOptions: React.FC<Props> = observer((props) => {
|
2023-12-06 08:52:59 +00:00
|
|
|
const { handleSignInRedirection } = props;
|
2023-11-29 13:37:33 +00:00
|
|
|
// toast alert
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
// mobx store
|
|
|
|
const {
|
|
|
|
appConfig: { envConfig },
|
|
|
|
} = useMobxStore();
|
|
|
|
|
|
|
|
const handleGoogleSignIn = async ({ clientId, credential }: any) => {
|
|
|
|
try {
|
|
|
|
if (clientId && credential) {
|
|
|
|
const socialAuthPayload = {
|
|
|
|
medium: "google",
|
|
|
|
credential,
|
|
|
|
clientId,
|
|
|
|
};
|
|
|
|
const response = await authService.socialAuth(socialAuthPayload);
|
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
if (response) handleSignInRedirection();
|
2023-11-29 13:37:33 +00:00
|
|
|
} else throw Error("Cant find credentials");
|
|
|
|
} catch (err: any) {
|
|
|
|
setToastAlert({
|
|
|
|
title: "Error signing in!",
|
|
|
|
type: "error",
|
|
|
|
message: err?.error || "Something went wrong. Please try again later or contact the support team.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleGitHubSignIn = async (credential: string) => {
|
|
|
|
try {
|
|
|
|
if (envConfig && envConfig.github_client_id && credential) {
|
|
|
|
const socialAuthPayload = {
|
|
|
|
medium: "github",
|
|
|
|
credential,
|
|
|
|
clientId: envConfig.github_client_id,
|
|
|
|
};
|
|
|
|
const response = await authService.socialAuth(socialAuthPayload);
|
|
|
|
|
2023-12-06 08:52:59 +00:00
|
|
|
if (response) handleSignInRedirection();
|
2023-11-29 13:37:33 +00:00
|
|
|
} else throw Error("Cant find credentials");
|
|
|
|
} catch (err: any) {
|
|
|
|
setToastAlert({
|
|
|
|
title: "Error signing in!",
|
|
|
|
type: "error",
|
|
|
|
message: err?.error || "Something went wrong. Please try again later or contact the support team.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-12-10 10:18:10 +00:00
|
|
|
<div className="mx-auto mt-4 flex items-center sm:w-96">
|
|
|
|
<hr className="w-full border-onboarding-border-100" />
|
|
|
|
<p className="mx-3 flex-shrink-0 text-center text-sm text-onboarding-text-400">Or continue with</p>
|
|
|
|
<hr className="w-full border-onboarding-border-100" />
|
2023-11-29 13:37:33 +00:00
|
|
|
</div>
|
2023-12-11 10:22:21 +00:00
|
|
|
<div className="mx-auto space-y-4 overflow-hidden mt-7 sm:w-96">
|
2023-11-29 13:37:33 +00:00
|
|
|
{envConfig?.google_client_id && (
|
2023-12-11 10:22:21 +00:00
|
|
|
<GoogleSignInButton clientId={envConfig?.google_client_id} handleSignIn={handleGoogleSignIn} />
|
2023-11-29 13:37:33 +00:00
|
|
|
)}
|
|
|
|
{envConfig?.github_client_id && (
|
2023-12-11 10:22:21 +00:00
|
|
|
<GitHubSignInButton clientId={envConfig?.github_client_id} handleSignIn={handleGitHubSignIn} />
|
2023-11-29 13:37:33 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|