Created a system to go directly to an assignment from a URL

This commit is contained in:
Tiago Ribeiro
2024-10-17 18:24:39 +01:00
parent a0a9402945
commit 4917583c67
10 changed files with 232 additions and 57 deletions

View File

@@ -20,16 +20,17 @@ import { redirect } from "@/utils";
const EMAIL_REGEX = new RegExp(/^[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*$/g);
export const getServerSideProps = withIronSessionSsr(async ({req, res}) => {
export const getServerSideProps = withIronSessionSsr(async ({req, res, query}) => {
const destination = !query.destination ? "/" : Buffer.from(query.destination as string, 'base64').toString()
const user = await requestUser(req, res)
if (user) return redirect("/")
if (user) return redirect(destination)
return {
props: {user: null},
props: {user: null, destination},
};
}, sessionOptions);
export default function Login() {
export default function Login({ destination }: { destination: string }) {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [rememberPassword, setRememberPassword] = useState(false);
@@ -38,13 +39,13 @@ export default function Login() {
const router = useRouter();
const {user, mutateUser} = useUser({
redirectTo: "/",
redirectTo: destination,
redirectIfFound: true,
});
useEffect(() => {
if (user) router.push("/");
}, [router, user]);
if (user) router.push(destination);
}, [router, user, destination]);
const forgotPassword = () => {
if (!email || email.length < 0 || !EMAIL_REGEX.test(email)) {