forked from github/plane
refactor: custom hook for sign in redirection (#2969)
This commit is contained in:
parent
79fa860b28
commit
fa990ed444
@ -84,7 +84,7 @@ export const EmailForm: React.FC<Props> = (props) => {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<h1 className="text-center text-2xl sm:text-2.5xl font-medium text-onboarding-text-100">
|
<h1 className="text-center text-2xl sm:text-2.5xl font-medium text-onboarding-text-100">
|
||||||
Get on your flight deck!
|
Get on your flight deck
|
||||||
</h1>
|
</h1>
|
||||||
<p className="text-center text-sm text-onboarding-text-200 mt-3">
|
<p className="text-center text-sm text-onboarding-text-200 mt-3">
|
||||||
Sign in with the email you used to sign up for Plane
|
Sign in with the email you used to sign up for Plane
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import React from "react";
|
import React, { useState } from "react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { Controller, useForm } from "react-hook-form";
|
import { Controller, useForm } from "react-hook-form";
|
||||||
import { XCircle } from "lucide-react";
|
import { XCircle } from "lucide-react";
|
||||||
@ -36,6 +36,8 @@ const authService = new AuthService();
|
|||||||
|
|
||||||
export const PasswordForm: React.FC<Props> = (props) => {
|
export const PasswordForm: React.FC<Props> = (props) => {
|
||||||
const { email, updateEmail, handleStepChange, handleSignInRedirection } = props;
|
const { email, updateEmail, handleStepChange, handleSignInRedirection } = props;
|
||||||
|
// states
|
||||||
|
const [isSendingResetPasswordLink, setIsSendingResetPasswordLink] = useState(false);
|
||||||
// toast alert
|
// toast alert
|
||||||
const { setToastAlert } = useToast();
|
const { setToastAlert } = useToast();
|
||||||
// form info
|
// form info
|
||||||
@ -113,6 +115,8 @@ export const PasswordForm: React.FC<Props> = (props) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setIsSendingResetPasswordLink(true);
|
||||||
|
|
||||||
authService
|
authService
|
||||||
.sendResetPasswordLink({ email: emailFormValue })
|
.sendResetPasswordLink({ email: emailFormValue })
|
||||||
.then(() => handleStepChange(ESignInSteps.SET_PASSWORD_LINK))
|
.then(() => handleStepChange(ESignInSteps.SET_PASSWORD_LINK))
|
||||||
@ -122,7 +126,8 @@ export const PasswordForm: React.FC<Props> = (props) => {
|
|||||||
title: "Error!",
|
title: "Error!",
|
||||||
message: err?.error ?? "Something went wrong. Please try again.",
|
message: err?.error ?? "Something went wrong. Please try again.",
|
||||||
})
|
})
|
||||||
);
|
)
|
||||||
|
.finally(() => setIsSendingResetPasswordLink(false));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -189,9 +194,12 @@ export const PasswordForm: React.FC<Props> = (props) => {
|
|||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={handleForgotPassword}
|
onClick={handleForgotPassword}
|
||||||
className="text-xs font-medium text-custom-primary-100"
|
className={`text-xs font-medium ${
|
||||||
|
isSendingResetPasswordLink ? "text-onboarding-text-300" : "text-custom-primary-100"
|
||||||
|
}`}
|
||||||
|
disabled={isSendingResetPasswordLink}
|
||||||
>
|
>
|
||||||
Forgot your password?
|
{isSendingResetPasswordLink ? "Sending link..." : "Forgot your password?"}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
|
// hooks
|
||||||
|
import useSignInRedirection from "hooks/use-sign-in-redirection";
|
||||||
// components
|
// components
|
||||||
import {
|
import {
|
||||||
EmailForm,
|
EmailForm,
|
||||||
@ -19,33 +21,27 @@ export enum ESignInSteps {
|
|||||||
CREATE_PASSWORD = "CREATE_PASSWORD",
|
CREATE_PASSWORD = "CREATE_PASSWORD",
|
||||||
}
|
}
|
||||||
|
|
||||||
type Props = {
|
|
||||||
handleSignInRedirection: () => Promise<void>;
|
|
||||||
};
|
|
||||||
|
|
||||||
const OAUTH_HIDDEN_STEPS = [ESignInSteps.OPTIONAL_SET_PASSWORD, ESignInSteps.CREATE_PASSWORD];
|
const OAUTH_HIDDEN_STEPS = [ESignInSteps.OPTIONAL_SET_PASSWORD, ESignInSteps.CREATE_PASSWORD];
|
||||||
|
|
||||||
export const SignInRoot: React.FC<Props> = (props) => {
|
export const SignInRoot = () => {
|
||||||
const { handleSignInRedirection } = props;
|
|
||||||
// states
|
// states
|
||||||
const [signInStep, setSignInStep] = useState<ESignInSteps>(ESignInSteps.EMAIL);
|
const [signInStep, setSignInStep] = useState<ESignInSteps>(ESignInSteps.EMAIL);
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
|
// sign in redirection hook
|
||||||
|
const { handleRedirection } = useSignInRedirection();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="mx-auto flex flex-col">
|
<div className="mx-auto flex flex-col">
|
||||||
{signInStep === ESignInSteps.EMAIL && (
|
{signInStep === ESignInSteps.EMAIL && (
|
||||||
<EmailForm
|
<EmailForm handleStepChange={(step) => setSignInStep(step)} updateEmail={(newEmail) => setEmail(newEmail)} />
|
||||||
handleStepChange={(step: ESignInSteps) => setSignInStep(step)}
|
|
||||||
updateEmail={(newEmail) => setEmail(newEmail)}
|
|
||||||
/>
|
|
||||||
)}
|
)}
|
||||||
{signInStep === ESignInSteps.PASSWORD && (
|
{signInStep === ESignInSteps.PASSWORD && (
|
||||||
<PasswordForm
|
<PasswordForm
|
||||||
email={email}
|
email={email}
|
||||||
updateEmail={(newEmail) => setEmail(newEmail)}
|
updateEmail={(newEmail) => setEmail(newEmail)}
|
||||||
handleStepChange={(step: ESignInSteps) => setSignInStep(step)}
|
handleStepChange={(step) => setSignInStep(step)}
|
||||||
handleSignInRedirection={handleSignInRedirection}
|
handleSignInRedirection={handleRedirection}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{signInStep === ESignInSteps.SET_PASSWORD_LINK && (
|
{signInStep === ESignInSteps.SET_PASSWORD_LINK && (
|
||||||
@ -55,30 +51,30 @@ export const SignInRoot: React.FC<Props> = (props) => {
|
|||||||
<UniqueCodeForm
|
<UniqueCodeForm
|
||||||
email={email}
|
email={email}
|
||||||
updateEmail={(newEmail) => setEmail(newEmail)}
|
updateEmail={(newEmail) => setEmail(newEmail)}
|
||||||
handleStepChange={(step: ESignInSteps) => setSignInStep(step)}
|
handleStepChange={(step) => setSignInStep(step)}
|
||||||
handleSignInRedirection={handleSignInRedirection}
|
handleSignInRedirection={handleRedirection}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{signInStep === ESignInSteps.OPTIONAL_SET_PASSWORD && (
|
{signInStep === ESignInSteps.OPTIONAL_SET_PASSWORD && (
|
||||||
<OptionalSetPasswordForm
|
<OptionalSetPasswordForm
|
||||||
email={email}
|
email={email}
|
||||||
handleStepChange={(step: ESignInSteps) => setSignInStep(step)}
|
handleStepChange={(step) => setSignInStep(step)}
|
||||||
handleSignInRedirection={handleSignInRedirection}
|
handleSignInRedirection={handleRedirection}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{signInStep === ESignInSteps.CREATE_PASSWORD && (
|
{signInStep === ESignInSteps.CREATE_PASSWORD && (
|
||||||
<CreatePasswordForm
|
<CreatePasswordForm
|
||||||
email={email}
|
email={email}
|
||||||
handleStepChange={(step: ESignInSteps) => setSignInStep(step)}
|
handleStepChange={(step) => setSignInStep(step)}
|
||||||
handleSignInRedirection={handleSignInRedirection}
|
handleSignInRedirection={handleRedirection}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
{!OAUTH_HIDDEN_STEPS.includes(signInStep) && (
|
{!OAUTH_HIDDEN_STEPS.includes(signInStep) && (
|
||||||
<OAuthOptions
|
<OAuthOptions
|
||||||
updateEmail={(newEmail) => setEmail(newEmail)}
|
updateEmail={(newEmail) => setEmail(newEmail)}
|
||||||
handleStepChange={(step: ESignInSteps) => setSignInStep(step)}
|
handleStepChange={(step) => setSignInStep(step)}
|
||||||
handleSignInRedirection={handleSignInRedirection}
|
handleSignInRedirection={handleRedirection}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
|
@ -30,7 +30,7 @@ export const SetPasswordLink: React.FC<Props> = (props) => {
|
|||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
formState: { errors, isValid },
|
formState: { errors, isValid },
|
||||||
watch,
|
handleSubmit,
|
||||||
} = useForm({
|
} = useForm({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
email,
|
email,
|
||||||
@ -39,11 +39,13 @@ export const SetPasswordLink: React.FC<Props> = (props) => {
|
|||||||
reValidateMode: "onChange",
|
reValidateMode: "onChange",
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSendNewLink = async () => {
|
const handleSendNewLink = async (formData: { email: string }) => {
|
||||||
setIsSendingNewLink(true);
|
setIsSendingNewLink(true);
|
||||||
|
|
||||||
|
updateEmail(formData.email);
|
||||||
|
|
||||||
const payload: IEmailCheckData = {
|
const payload: IEmailCheckData = {
|
||||||
email: watch("email"),
|
email: formData.email,
|
||||||
type: "password",
|
type: "password",
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -76,7 +78,7 @@ export const SetPasswordLink: React.FC<Props> = (props) => {
|
|||||||
password
|
password
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<form className="mt-5 sm:w-96 mx-auto space-y-4">
|
<form onSubmit={handleSubmit(handleSendNewLink)} className="mt-5 sm:w-96 mx-auto space-y-4">
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<Controller
|
<Controller
|
||||||
control={control}
|
control={control}
|
||||||
@ -92,10 +94,7 @@ export const SetPasswordLink: React.FC<Props> = (props) => {
|
|||||||
name="email"
|
name="email"
|
||||||
type="email"
|
type="email"
|
||||||
value={value}
|
value={value}
|
||||||
onChange={(e) => {
|
onChange={onChange}
|
||||||
updateEmail(e.target.value);
|
|
||||||
onChange(e.target.value);
|
|
||||||
}}
|
|
||||||
ref={ref}
|
ref={ref}
|
||||||
hasError={Boolean(errors.email)}
|
hasError={Boolean(errors.email)}
|
||||||
placeholder="orville.wright@firstflight.com"
|
placeholder="orville.wright@firstflight.com"
|
||||||
@ -112,11 +111,10 @@ export const SetPasswordLink: React.FC<Props> = (props) => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="submit"
|
||||||
variant="primary"
|
variant="primary"
|
||||||
className="w-full"
|
className="w-full"
|
||||||
size="xl"
|
size="xl"
|
||||||
onClick={handleSendNewLink}
|
|
||||||
disabled={!isValid}
|
disabled={!isValid}
|
||||||
loading={isSendingNewLink}
|
loading={isSendingNewLink}
|
||||||
>
|
>
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
import { useState, useEffect, useCallback } from "react";
|
import { useEffect } from "react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { observer } from "mobx-react-lite";
|
import { observer } from "mobx-react-lite";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { useTheme } from "next-themes";
|
import { useTheme } from "next-themes";
|
||||||
import { useRouter } from "next/router";
|
|
||||||
import { Lightbulb } from "lucide-react";
|
import { Lightbulb } from "lucide-react";
|
||||||
// mobx store
|
// mobx store
|
||||||
import { useMobxStore } from "lib/mobx/store-provider";
|
import { useMobxStore } from "lib/mobx/store-provider";
|
||||||
|
// hooks
|
||||||
|
import useSignInRedirection from "hooks/use-sign-in-redirection";
|
||||||
// components
|
// components
|
||||||
import { SignInRoot } from "components/account";
|
import { SignInRoot } from "components/account";
|
||||||
// ui
|
// ui
|
||||||
@ -14,122 +15,84 @@ import { Loader, Spinner } from "@plane/ui";
|
|||||||
// images
|
// images
|
||||||
import BluePlaneLogoWithoutText from "public/plane-logos/blue-without-text.png";
|
import BluePlaneLogoWithoutText from "public/plane-logos/blue-without-text.png";
|
||||||
import latestFeatures from "public/onboarding/onboarding-pages.svg";
|
import latestFeatures from "public/onboarding/onboarding-pages.svg";
|
||||||
// types
|
|
||||||
import { IUser, IUserSettings } from "types";
|
|
||||||
|
|
||||||
export type AuthType = "sign-in" | "sign-up";
|
export type AuthType = "sign-in" | "sign-up";
|
||||||
|
|
||||||
export const SignInView = observer(() => {
|
export const SignInView = observer(() => {
|
||||||
// store
|
// store
|
||||||
const {
|
const {
|
||||||
user: { fetchCurrentUser, fetchCurrentUserSettings },
|
user: { currentUser },
|
||||||
appConfig: { envConfig },
|
appConfig: { envConfig },
|
||||||
} = useMobxStore();
|
} = useMobxStore();
|
||||||
// router
|
|
||||||
const router = useRouter();
|
|
||||||
const { next: next_url } = router.query;
|
|
||||||
// states
|
|
||||||
const [isLoading, setLoading] = useState(false);
|
|
||||||
// next-themes
|
// next-themes
|
||||||
const { resolvedTheme } = useTheme();
|
const { resolvedTheme } = useTheme();
|
||||||
|
// sign in redirection hook
|
||||||
const handleSignInRedirection = useCallback(
|
const { isRedirecting, handleRedirection } = useSignInRedirection();
|
||||||
async (user: IUser) => {
|
|
||||||
// if the user is not onboarded, redirect them to the onboarding page
|
|
||||||
if (!user.is_onboarded) {
|
|
||||||
router.push("/onboarding");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// if next_url is provided, redirect the user to that url
|
|
||||||
if (next_url) {
|
|
||||||
router.push(next_url.toString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the user is onboarded, fetch their last workspace details
|
|
||||||
await fetchCurrentUserSettings()
|
|
||||||
.then((userSettings: IUserSettings) => {
|
|
||||||
const workspaceSlug =
|
|
||||||
userSettings?.workspace?.last_workspace_slug || userSettings?.workspace?.fallback_workspace_slug;
|
|
||||||
if (workspaceSlug) router.push(`/${workspaceSlug}`);
|
|
||||||
else router.push("/profile");
|
|
||||||
})
|
|
||||||
.catch(() => setLoading(false));
|
|
||||||
},
|
|
||||||
[fetchCurrentUserSettings, router, next_url]
|
|
||||||
);
|
|
||||||
|
|
||||||
const mutateUserInfo = useCallback(async () => {
|
|
||||||
await fetchCurrentUser().then(async (user) => {
|
|
||||||
await handleSignInRedirection(user);
|
|
||||||
});
|
|
||||||
}, [fetchCurrentUser, handleSignInRedirection]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
mutateUserInfo();
|
handleRedirection();
|
||||||
}, [mutateUserInfo]);
|
}, [handleRedirection]);
|
||||||
|
|
||||||
|
if (isRedirecting || currentUser)
|
||||||
|
return (
|
||||||
|
<div className="grid place-items-center h-screen">
|
||||||
|
<Spinner />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<div className="bg-onboarding-gradient-100 h-full w-full">
|
||||||
{isLoading ? (
|
<div className="flex items-center justify-between sm:py-5 px-8 pb-4 sm:px-16 lg:px-28 ">
|
||||||
<div className="grid place-items-center h-screen">
|
<div className="flex gap-x-2 py-10 items-center">
|
||||||
<Spinner />
|
<Image src={BluePlaneLogoWithoutText} height={30} width={30} alt="Plane Logo" className="mr-2" />
|
||||||
|
<span className="font-semibold text-2xl sm:text-3xl">Plane</span>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
</div>
|
||||||
<div className={`bg-onboarding-gradient-100 h-full w-full`}>
|
|
||||||
<div className="flex items-center justify-between sm:py-5 px-8 pb-4 sm:px-16 lg:px-28 ">
|
<div className="h-full bg-onboarding-gradient-100 md:w-2/3 sm:w-4/5 px-4 pt-4 rounded-t-md mx-auto shadow-sm border-x border-t border-custom-border-200 ">
|
||||||
<div className="flex gap-x-2 py-10 items-center">
|
<div className="px-7 sm:px-0 bg-onboarding-gradient-200 h-full pt-24 pb-56 rounded-t-md overflow-auto">
|
||||||
<Image src={BluePlaneLogoWithoutText} height={30} width={30} alt="Plane Logo" className="mr-2" />
|
{!envConfig ? (
|
||||||
<span className="font-semibold text-2xl sm:text-3xl">Plane</span>
|
<div className="pt-10 mx-auto flex justify-center">
|
||||||
|
<div>
|
||||||
|
<Loader className="space-y-4 w-full pb-4 mx-auto">
|
||||||
|
<Loader.Item height="46px" width="360px" />
|
||||||
|
<Loader.Item height="46px" width="360px" />
|
||||||
|
</Loader>
|
||||||
|
|
||||||
|
<Loader className="space-y-4 w-full pt-4 mx-auto">
|
||||||
|
<Loader.Item height="46px" width="360px" />
|
||||||
|
<Loader.Item height="46px" width="360px" />
|
||||||
|
</Loader>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
) : (
|
||||||
|
<>
|
||||||
|
<SignInRoot />
|
||||||
|
|
||||||
<div className="h-full bg-onboarding-gradient-100 md:w-2/3 sm:w-4/5 px-4 pt-4 rounded-t-md mx-auto shadow-sm border-x border-t border-custom-border-200 ">
|
<div className="flex py-2 bg-onboarding-background-100 border border-onboarding-border-200 mx-auto rounded-[3.5px] sm:w-96 mt-16">
|
||||||
<div className="px-7 sm:px-0 bg-onboarding-gradient-200 h-full pt-24 pb-56 rounded-t-md overflow-auto">
|
<Lightbulb className="h-7 w-7 mr-2 mx-3" />
|
||||||
{!envConfig ? (
|
<p className="text-sm text-left text-onboarding-text-100">
|
||||||
<div className="pt-10 mx-auto flex justify-center">
|
Pages gets a facelift! Write anything and use Galileo to help you start.{" "}
|
||||||
<div>
|
<Link href="https://plane.so/changelog" target="_blank" rel="noopener noreferrer">
|
||||||
<Loader className="space-y-4 w-full pb-4 mx-auto">
|
<span className="font-medium text-sm underline hover:cursor-pointer">Learn more</span>
|
||||||
<Loader.Item height="46px" width="360px" />
|
</Link>
|
||||||
<Loader.Item height="46px" width="360px" />
|
</p>
|
||||||
</Loader>
|
</div>
|
||||||
|
<div className="border border-onboarding-border-200 sm:w-96 sm:h-52 object-cover mt-8 mx-auto rounded-md bg-onboarding-background-100 overflow-hidden">
|
||||||
<Loader className="space-y-4 w-full pt-4 mx-auto">
|
<div className="h-[90%]">
|
||||||
<Loader.Item height="46px" width="360px" />
|
<Image
|
||||||
<Loader.Item height="46px" width="360px" />
|
src={latestFeatures}
|
||||||
</Loader>
|
alt="Plane Issues"
|
||||||
</div>
|
className={`rounded-md h-full ml-8 -mt-2 ${
|
||||||
|
resolvedTheme === "dark" ? "bg-onboarding-background-100" : "bg-custom-primary-70"
|
||||||
|
} `}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
</div>
|
||||||
<>
|
</>
|
||||||
<SignInRoot handleSignInRedirection={mutateUserInfo} />
|
)}
|
||||||
|
|
||||||
<div className="flex py-2 bg-onboarding-background-100 border border-onboarding-border-200 mx-auto rounded-[3.5px] sm:w-96 mt-16">
|
|
||||||
<Lightbulb className="h-7 w-7 mr-2 mx-3" />
|
|
||||||
<p className="text-sm text-left text-onboarding-text-100">
|
|
||||||
Pages gets a facelift! Write anything and use Galileo to help you start.{" "}
|
|
||||||
<Link href="https://plane.so/changelog" target="_blank" rel="noopener noreferrer">
|
|
||||||
<span className="font-medium text-sm underline hover:cursor-pointer">Learn more</span>
|
|
||||||
</Link>
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div className="border border-onboarding-border-200 sm:w-96 sm:h-52 object-cover mt-8 mx-auto rounded-md bg-onboarding-background-100 overflow-hidden">
|
|
||||||
<div className="h-[90%]">
|
|
||||||
<Image
|
|
||||||
src={latestFeatures}
|
|
||||||
alt="Plane Issues"
|
|
||||||
className={`rounded-md h-full ml-8 -mt-2 ${
|
|
||||||
resolvedTheme === "dark" ? "bg-onboarding-background-100" : "bg-custom-primary-70"
|
|
||||||
} `}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
</div>
|
||||||
</>
|
</div>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
74
web/hooks/use-sign-in-redirection.ts
Normal file
74
web/hooks/use-sign-in-redirection.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import { useCallback, useState } from "react";
|
||||||
|
import { useRouter } from "next/router";
|
||||||
|
// mobx store
|
||||||
|
import { useMobxStore } from "lib/mobx/store-provider";
|
||||||
|
// types
|
||||||
|
import { IUser, IUserSettings } from "types";
|
||||||
|
|
||||||
|
type UseSignInRedirectionProps = {
|
||||||
|
error: any | null;
|
||||||
|
isRedirecting: boolean;
|
||||||
|
handleRedirection: () => Promise<void>;
|
||||||
|
};
|
||||||
|
|
||||||
|
const useSignInRedirection = (): UseSignInRedirectionProps => {
|
||||||
|
// states
|
||||||
|
const [isRedirecting, setIsRedirecting] = useState(true);
|
||||||
|
const [error, setError] = useState<any | null>(null);
|
||||||
|
// router
|
||||||
|
const router = useRouter();
|
||||||
|
const { next_url } = router.query;
|
||||||
|
// mobx store
|
||||||
|
const {
|
||||||
|
user: { fetchCurrentUser, fetchCurrentUserSettings },
|
||||||
|
} = useMobxStore();
|
||||||
|
|
||||||
|
const handleSignInRedirection = useCallback(
|
||||||
|
async (user: IUser) => {
|
||||||
|
// if the user is not onboarded, redirect them to the onboarding page
|
||||||
|
if (!user.is_onboarded) {
|
||||||
|
router.push("/onboarding");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// if next_url is provided, redirect the user to that url
|
||||||
|
if (next_url) {
|
||||||
|
router.push(next_url.toString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the user is onboarded, fetch their last workspace details
|
||||||
|
await fetchCurrentUserSettings()
|
||||||
|
.then((userSettings: IUserSettings) => {
|
||||||
|
const workspaceSlug =
|
||||||
|
userSettings?.workspace?.last_workspace_slug || userSettings?.workspace?.fallback_workspace_slug;
|
||||||
|
if (workspaceSlug) router.push(`/${workspaceSlug}`);
|
||||||
|
else router.push("/profile");
|
||||||
|
})
|
||||||
|
.catch((err) => setError(err));
|
||||||
|
},
|
||||||
|
[fetchCurrentUserSettings, router, next_url]
|
||||||
|
);
|
||||||
|
|
||||||
|
const updateUserInfo = useCallback(async () => {
|
||||||
|
setIsRedirecting(true);
|
||||||
|
|
||||||
|
await fetchCurrentUser()
|
||||||
|
.then(async (user) => {
|
||||||
|
await handleSignInRedirection(user)
|
||||||
|
.catch((err) => setError(err))
|
||||||
|
.finally(() => setIsRedirecting(false));
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
setError(err);
|
||||||
|
setIsRedirecting(false);
|
||||||
|
});
|
||||||
|
}, [fetchCurrentUser, handleSignInRedirection]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
error,
|
||||||
|
isRedirecting,
|
||||||
|
handleRedirection: updateUserInfo,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useSignInRedirection;
|
@ -1,4 +1,4 @@
|
|||||||
import { ReactElement, useCallback } from "react";
|
import { ReactElement } from "react";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
@ -9,6 +9,7 @@ import { Controller, useForm } from "react-hook-form";
|
|||||||
import { AuthService } from "services/auth.service";
|
import { AuthService } from "services/auth.service";
|
||||||
// hooks
|
// hooks
|
||||||
import useToast from "hooks/use-toast";
|
import useToast from "hooks/use-toast";
|
||||||
|
import useSignInRedirection from "hooks/use-sign-in-redirection";
|
||||||
// layouts
|
// layouts
|
||||||
import DefaultLayout from "layouts/default-layout";
|
import DefaultLayout from "layouts/default-layout";
|
||||||
// ui
|
// ui
|
||||||
@ -20,9 +21,6 @@ import latestFeatures from "public/onboarding/onboarding-pages.svg";
|
|||||||
import { checkEmailValidity } from "helpers/string.helper";
|
import { checkEmailValidity } from "helpers/string.helper";
|
||||||
// type
|
// type
|
||||||
import { NextPageWithLayout } from "types/app";
|
import { NextPageWithLayout } from "types/app";
|
||||||
import { useMobxStore } from "lib/mobx/store-provider";
|
|
||||||
// types
|
|
||||||
import { IUser, IUserSettings } from "types";
|
|
||||||
|
|
||||||
type TResetPasswordFormValues = {
|
type TResetPasswordFormValues = {
|
||||||
email: string;
|
email: string;
|
||||||
@ -45,10 +43,8 @@ const HomePage: NextPageWithLayout = () => {
|
|||||||
const { resolvedTheme } = useTheme();
|
const { resolvedTheme } = useTheme();
|
||||||
// toast
|
// toast
|
||||||
const { setToastAlert } = useToast();
|
const { setToastAlert } = useToast();
|
||||||
// mobx store
|
// sign in redirection hook
|
||||||
const {
|
const { handleRedirection } = useSignInRedirection();
|
||||||
user: { fetchCurrentUser, fetchCurrentUserSettings },
|
|
||||||
} = useMobxStore();
|
|
||||||
// form info
|
// form info
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
@ -61,31 +57,6 @@ const HomePage: NextPageWithLayout = () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSignInRedirection = useCallback(
|
|
||||||
async (user: IUser) => {
|
|
||||||
// if the user is not onboarded, redirect them to the onboarding page
|
|
||||||
if (!user.is_onboarded) {
|
|
||||||
router.push("/onboarding");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the user is onboarded, fetch their last workspace details
|
|
||||||
await fetchCurrentUserSettings().then((userSettings: IUserSettings) => {
|
|
||||||
const workspaceSlug =
|
|
||||||
userSettings?.workspace?.last_workspace_slug || userSettings?.workspace?.fallback_workspace_slug;
|
|
||||||
if (workspaceSlug) router.push(`/${workspaceSlug}`);
|
|
||||||
else router.push("/profile");
|
|
||||||
});
|
|
||||||
},
|
|
||||||
[fetchCurrentUserSettings, router]
|
|
||||||
);
|
|
||||||
|
|
||||||
const mutateUserInfo = useCallback(async () => {
|
|
||||||
await fetchCurrentUser().then(async (user) => {
|
|
||||||
await handleSignInRedirection(user);
|
|
||||||
});
|
|
||||||
}, [fetchCurrentUser, handleSignInRedirection]);
|
|
||||||
|
|
||||||
const handleResetPassword = async (formData: TResetPasswordFormValues) => {
|
const handleResetPassword = async (formData: TResetPasswordFormValues) => {
|
||||||
if (!uidb64 || !token || !email) return;
|
if (!uidb64 || !token || !email) return;
|
||||||
|
|
||||||
@ -95,7 +66,7 @@ const HomePage: NextPageWithLayout = () => {
|
|||||||
|
|
||||||
await authService
|
await authService
|
||||||
.resetPassword(uidb64.toString(), token.toString(), payload)
|
.resetPassword(uidb64.toString(), token.toString(), payload)
|
||||||
.then(() => mutateUserInfo())
|
.then(() => handleRedirection())
|
||||||
.catch((err) =>
|
.catch((err) =>
|
||||||
setToastAlert({
|
setToastAlert({
|
||||||
type: "error",
|
type: "error",
|
||||||
|
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 527 KiB After Width: | Height: | Size: 528 KiB |
Loading…
Reference in New Issue
Block a user