diff --git a/apps/app/pages/magic-sign-in.tsx b/apps/app/pages/magic-sign-in.tsx index 0a2c2493f1..81ebf02670 100644 --- a/apps/app/pages/magic-sign-in.tsx +++ b/apps/app/pages/magic-sign-in.tsx @@ -1,67 +1,63 @@ import React, { useState, useEffect } from "react"; - +// next imports import { useRouter } from "next/router"; - -import { mutate } from "swr"; - // layouts import DefaultLayout from "layouts/default-layout"; // services import authenticationService from "services/authentication.service"; // hooks -import useUser from "hooks/use-user"; +import useUserAuth from "hooks/use-user-auth"; import useToast from "hooks/use-toast"; // types import type { NextPage } from "next"; -// constants -import { USER_WORKSPACES } from "constants/fetch-keys"; 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(); + const { user, isLoading, mutateUser } = useUserAuth("sign-in"); + + const [isSigningIn, setIsSigningIn] = useState(false); + const [errorSigningIn, setErrorSignIn] = useState(); 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); - }); + setIsSigningIn(() => false); + setErrorSignIn(() => undefined); + if (!password || !key) { + setErrorSignIn("URL is invalid"); + return; + } else { + setIsSigningIn(() => true); + authenticationService + .magicSignIn({ token: password, key }) + .then(async (res) => { + setIsSigningIn(false); + await mutateUser(); + }) + .catch((err) => { + setErrorSignIn(err.response.data.error); + setIsSigningIn(false); + }); + } }, [password, key, mutateUser, router]); return (
{isSigningIn ? ( -
-

Signing you in...

-

+

+

Signing you in...

+

Please wait while we are preparing your take off.

) : errorSigningIn ? ( -
-

Error

-

- {errorSigningIn}. +

+

Error

+
+
{errorSigningIn}.
{ @@ -85,12 +81,14 @@ const MagicSignIn: NextPage = () => { > Send link again? -

+
) : (
-

Success

-

Redirecting you to the app...

+

Success

+

+ Redirecting you to the app... +

)}
diff --git a/apps/app/services/api.service.ts b/apps/app/services/api.service.ts index b9d993548f..ef198c221e 100644 --- a/apps/app/services/api.service.ts +++ b/apps/app/services/api.service.ts @@ -2,7 +2,12 @@ import axios from "axios"; import Cookies from "js-cookie"; const unAuthorizedStatus = [401]; -const nonValidatedRoutes = ["/", "/reset-password", "/workspace-member-invitation"]; +const nonValidatedRoutes = [ + "/", + "/magic-sign-in", + "/reset-password", + "/workspace-member-invitation", +]; const validateRouteCheck = (route: string): boolean => { let validationToggle = false;