import React, { useState, useEffect } from "react"; import type { NextPage } from "next"; import { useRouter } from "next/router"; import { mutate } from "swr"; // services import authenticationService from "lib/services/authentication.service"; // constants import { USER_WORKSPACES } from "constants/fetch-keys"; // hooks import useUser from "lib/hooks/useUser"; import useToast from "lib/hooks/useToast"; // layouts import DefaultLayout from "layouts/default-layout"; const MagicSignIn: NextPage = () => { const router = useRouter(); const [isSigningIn, setIsSigningIn] = useState(true); const [errorSigningIn, setErrorSignIn] = useState(); const { password, key } = router.query; const { setToastAlert } = useToast(); const { mutateUser } = useUser(); useEffect(() => { setIsSigningIn(true); setErrorSignIn(undefined); if (!password || !key) return; authenticationService .magicSignIn({ token: password, key }) .then(async (res) => { setIsSigningIn(false); await mutateUser(); mutate(USER_WORKSPACES); if (res.user.is_onboarded) router.push("/"); else router.push("/invitations"); }) .catch((err) => { setErrorSignIn(err.response.data.error); setIsSigningIn(false); }); }, [password, key, mutateUser, router]); return (
{isSigningIn ? (

Signing you in...

Please wait while we are preparing your take off.

) : errorSigningIn ? (

Error

{errorSigningIn}. { authenticationService .emailCode({ email: (key as string).split("_")[1] }) .then(() => { setToastAlert({ type: "success", title: "Email sent", message: "A new link/code has been send to you.", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error", message: "Unable to send email.", }); }); }} > Send link again?

) : (

Success

Redirecting you to the app...

)}
); }; export default MagicSignIn;