2023-05-30 13:44:35 +00:00
|
|
|
import React from "react";
|
2023-07-17 07:30:44 +00:00
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
import Image from "next/image";
|
2023-07-17 07:30:44 +00:00
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
import type { NextPage } from "next";
|
2023-07-17 07:30:44 +00:00
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
// layouts
|
|
|
|
import DefaultLayout from "layouts/default-layout";
|
2023-07-17 07:30:44 +00:00
|
|
|
// services
|
|
|
|
import authenticationService from "services/authentication.service";
|
2023-05-30 13:44:35 +00:00
|
|
|
// hooks
|
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-07-17 07:30:44 +00:00
|
|
|
// components
|
2023-05-30 13:44:35 +00:00
|
|
|
import {
|
|
|
|
GoogleLoginButton,
|
|
|
|
GithubLoginButton,
|
|
|
|
EmailCodeForm,
|
|
|
|
EmailPasswordForm,
|
|
|
|
} from "components/account";
|
2023-04-08 18:32:33 +00:00
|
|
|
// ui
|
|
|
|
import { Spinner } from "components/ui";
|
2023-07-17 07:30:44 +00:00
|
|
|
// images
|
|
|
|
import BluePlaneLogoWithoutText from "public/plane-logos/blue-without-text.png";
|
2023-06-16 13:30:04 +00:00
|
|
|
// types
|
|
|
|
type EmailPasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password?: string;
|
|
|
|
medium?: string;
|
|
|
|
};
|
2023-04-13 10:08:00 +00:00
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
const HomePage: NextPage = () => {
|
2023-07-17 07:30:44 +00:00
|
|
|
const { isLoading, mutateUser } = useUserAuth("sign-in");
|
2023-04-08 18:32:33 +00:00
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2023-04-12 13:33:08 +00:00
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
const handleGoogleSignIn = async ({ clientId, credential }: any) => {
|
|
|
|
try {
|
|
|
|
if (clientId && credential) {
|
2023-06-09 19:56:38 +00:00
|
|
|
const socialAuthPayload = {
|
|
|
|
medium: "google",
|
|
|
|
credential,
|
|
|
|
clientId,
|
|
|
|
};
|
|
|
|
const response = await authenticationService.socialAuth(socialAuthPayload);
|
|
|
|
if (response && response?.user) mutateUser();
|
2023-04-13 10:08:00 +00:00
|
|
|
} else {
|
2023-05-30 13:44:35 +00:00
|
|
|
throw Error("Cant find credentials");
|
2023-04-13 10:08:00 +00:00
|
|
|
}
|
2023-06-23 14:00:32 +00:00
|
|
|
} catch (err: any) {
|
2023-05-30 13:44:35 +00:00
|
|
|
setToastAlert({
|
|
|
|
title: "Error signing in!",
|
|
|
|
type: "error",
|
2023-06-13 09:07:25 +00:00
|
|
|
message:
|
2023-06-23 14:00:32 +00:00
|
|
|
err?.error || "Something went wrong. Please try again later or contact the support team.",
|
2023-05-30 13:44:35 +00:00
|
|
|
});
|
2023-04-08 18:32:33 +00:00
|
|
|
}
|
2023-05-30 13:44:35 +00:00
|
|
|
};
|
2023-04-13 10:08:00 +00:00
|
|
|
|
2023-07-17 07:30:44 +00:00
|
|
|
const handleGitHubSignIn = async (credential: string) => {
|
2023-05-30 13:44:35 +00:00
|
|
|
try {
|
|
|
|
if (process.env.NEXT_PUBLIC_GITHUB_ID && credential) {
|
2023-06-09 19:56:38 +00:00
|
|
|
const socialAuthPayload = {
|
|
|
|
medium: "github",
|
|
|
|
credential,
|
|
|
|
clientId: process.env.NEXT_PUBLIC_GITHUB_ID,
|
|
|
|
};
|
|
|
|
const response = await authenticationService.socialAuth(socialAuthPayload);
|
|
|
|
if (response && response?.user) mutateUser();
|
2023-05-30 13:44:35 +00:00
|
|
|
} else {
|
|
|
|
throw Error("Cant find credentials");
|
|
|
|
}
|
2023-06-23 14:00:32 +00:00
|
|
|
} catch (err: any) {
|
2023-05-30 13:44:35 +00:00
|
|
|
setToastAlert({
|
|
|
|
title: "Error signing in!",
|
|
|
|
type: "error",
|
2023-06-13 09:07:25 +00:00
|
|
|
message:
|
2023-06-23 14:00:32 +00:00
|
|
|
err?.error || "Something went wrong. Please try again later or contact the support team.",
|
2023-05-30 13:44:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2023-06-16 13:30:04 +00:00
|
|
|
const handlePasswordSignIn = async (formData: EmailPasswordFormValues) => {
|
|
|
|
await authenticationService
|
|
|
|
.emailLogin(formData)
|
|
|
|
.then((response) => {
|
|
|
|
try {
|
|
|
|
if (response) mutateUser();
|
2023-06-23 14:00:32 +00:00
|
|
|
} catch (err: any) {
|
2023-06-16 13:30:04 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
2023-06-23 14:00:32 +00:00
|
|
|
title: "Error!",
|
2023-06-16 13:30:04 +00:00
|
|
|
message:
|
2023-06-23 14:00:32 +00:00
|
|
|
err?.error ||
|
2023-06-16 13:30:04 +00:00
|
|
|
"Something went wrong. Please try again later or contact the support team.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
2023-06-23 14:00:32 +00:00
|
|
|
.catch((err) =>
|
2023-06-16 13:30:04 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
2023-06-23 14:00:32 +00:00
|
|
|
title: "Error!",
|
|
|
|
message:
|
|
|
|
err?.error ||
|
|
|
|
"Something went wrong. Please try again later or contact the support team.",
|
2023-06-16 13:30:04 +00:00
|
|
|
})
|
|
|
|
);
|
2023-06-13 09:07:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleEmailCodeSignIn = async (response: any) => {
|
|
|
|
try {
|
|
|
|
if (response) mutateUser();
|
2023-06-23 14:00:32 +00:00
|
|
|
} catch (err: any) {
|
2023-06-13 09:07:25 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
2023-06-23 14:00:32 +00:00
|
|
|
title: "Error!",
|
2023-06-13 09:07:25 +00:00
|
|
|
message:
|
2023-06-23 14:00:32 +00:00
|
|
|
err?.error || "Something went wrong. Please try again later or contact the support team.",
|
2023-06-05 12:18:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-08 18:32:33 +00:00
|
|
|
return (
|
2023-05-30 13:44:35 +00:00
|
|
|
<DefaultLayout>
|
|
|
|
{isLoading ? (
|
2023-07-17 07:30:44 +00:00
|
|
|
<div className="grid place-items-center h-screen">
|
|
|
|
<Spinner />
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
2023-07-17 07:30:44 +00:00
|
|
|
<>
|
|
|
|
<>
|
|
|
|
<div className="hidden sm:block sm:fixed border-r-[0.5px] border-custom-border-200 h-screen w-[0.5px] top-0 left-20 lg:left-32" />
|
|
|
|
<div className="fixed grid place-items-center bg-custom-background-100 sm:py-5 top-11 sm:top-12 left-7 sm:left-16 lg:left-28">
|
|
|
|
<div className="grid place-items-center bg-custom-background-100">
|
|
|
|
<div className="h-[30px] w-[30px]">
|
|
|
|
<Image src={BluePlaneLogoWithoutText} alt="Plane Logo" />
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-17 07:30:44 +00:00
|
|
|
</div>
|
|
|
|
</>
|
2023-07-18 13:32:33 +00:00
|
|
|
<div className="grid place-items-center h-full overflow-y-auto py-5 px-7">
|
2023-07-17 07:30:44 +00:00
|
|
|
<div>
|
|
|
|
{parseInt(process.env.NEXT_PUBLIC_ENABLE_OAUTH || "0") ? (
|
|
|
|
<>
|
|
|
|
<h1 className="text-center text-2xl sm:text-2.5xl font-semibold text-custom-text-100">
|
|
|
|
Sign in to Plane
|
|
|
|
</h1>
|
2023-07-20 09:44:57 +00:00
|
|
|
<div className="flex flex-col divide-y divide-custom-border-200">
|
2023-07-17 07:30:44 +00:00
|
|
|
<div className="pb-7">
|
|
|
|
<EmailCodeForm handleSignIn={handleEmailCodeSignIn} />
|
|
|
|
</div>
|
2023-07-20 09:44:57 +00:00
|
|
|
<div className="flex flex-col items-center justify-center gap-4 pt-7 sm:w-[360px] mx-auto overflow-hidden">
|
2023-05-30 13:44:35 +00:00
|
|
|
<GoogleLoginButton handleSignIn={handleGoogleSignIn} />
|
2023-07-17 07:30:44 +00:00
|
|
|
<GithubLoginButton handleSignIn={handleGitHubSignIn} />
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
2023-07-17 07:30:44 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<EmailPasswordForm onSubmit={handlePasswordSignIn} />
|
|
|
|
)}
|
|
|
|
|
|
|
|
{parseInt(process.env.NEXT_PUBLIC_ENABLE_OAUTH || "0") ? (
|
|
|
|
<p className="pt-16 text-custom-text-200 text-sm text-center">
|
|
|
|
By signing up, you agree to the{" "}
|
|
|
|
<a
|
|
|
|
href="https://plane.so/terms-and-conditions"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
className="font-medium underline"
|
|
|
|
>
|
|
|
|
Terms & Conditions
|
|
|
|
</a>
|
|
|
|
</p>
|
|
|
|
) : null}
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-17 07:30:44 +00:00
|
|
|
</>
|
2023-05-30 13:44:35 +00:00
|
|
|
)}
|
|
|
|
</DefaultLayout>
|
2023-04-08 18:32:33 +00:00
|
|
|
);
|
2022-11-19 14:21:26 +00:00
|
|
|
};
|
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
export default HomePage;
|