2023-02-04 18:12:49 +01:00
|
|
|
import { LoadingOverlay } from "@mantine/core";
|
2023-02-07 10:21:25 +01:00
|
|
|
import { GetServerSidePropsContext } from "next";
|
2022-04-25 15:15:17 +02:00
|
|
|
import { useRouter } from "next/router";
|
2023-02-07 10:21:25 +01:00
|
|
|
import { useEffect, useState } from "react";
|
2022-12-01 23:07:49 +01:00
|
|
|
import SignInForm from "../../components/auth/SignInForm";
|
2022-04-28 15:31:37 +02:00
|
|
|
import Meta from "../../components/Meta";
|
2022-10-09 22:30:32 +02:00
|
|
|
import useUser from "../../hooks/user.hook";
|
2023-07-20 15:32:07 +02:00
|
|
|
import useTranslate from "../../hooks/useTranslate.hook";
|
2022-04-25 15:15:17 +02:00
|
|
|
|
2023-02-07 10:21:25 +01:00
|
|
|
export function getServerSideProps(context: GetServerSidePropsContext) {
|
|
|
|
return {
|
|
|
|
props: { redirectPath: context.query.redirect ?? null },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const SignIn = ({ redirectPath }: { redirectPath?: string }) => {
|
|
|
|
const { refreshUser } = useUser();
|
2023-02-06 11:15:46 +01:00
|
|
|
const router = useRouter();
|
2023-07-20 15:32:07 +02:00
|
|
|
const t = useTranslate();
|
2023-02-06 11:15:46 +01:00
|
|
|
|
2023-02-07 10:21:25 +01:00
|
|
|
const [isLoading, setIsLoading] = useState(redirectPath ? true : false);
|
2023-02-04 18:12:49 +01:00
|
|
|
|
|
|
|
// If the access token is expired, the middleware redirects to this page.
|
2023-02-07 10:21:25 +01:00
|
|
|
// If the refresh token is still valid, the user will be redirected to the last page.
|
|
|
|
useEffect(() => {
|
|
|
|
refreshUser().then((user) => {
|
|
|
|
if (user) {
|
|
|
|
router.replace(redirectPath ?? "/upload");
|
|
|
|
} else {
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (isLoading) return <LoadingOverlay overlayOpacity={1} visible />;
|
2023-02-04 18:12:49 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-07-20 15:32:07 +02:00
|
|
|
<Meta title={t("signin.title")} />
|
2023-02-07 10:21:25 +01:00
|
|
|
<SignInForm redirectPath={redirectPath ?? "/upload"} />
|
2023-02-04 18:12:49 +01:00
|
|
|
</>
|
|
|
|
);
|
2022-04-25 15:15:17 +02:00
|
|
|
};
|
|
|
|
export default SignIn;
|