chore: removed old auth hooks

This commit is contained in:
gurusainath 2024-05-02 14:52:18 +05:30
parent dd5c4a3aed
commit 4239dc0daf
11 changed files with 19 additions and 259 deletions

View File

@ -18,7 +18,6 @@ import { EXPORT_SERVICES_LIST } from "@/constants/fetch-keys";
import { EXPORTERS_LIST } from "@/constants/workspace";
// hooks
import { useUser } from "@/hooks/store";
import useUserAuth from "@/hooks/use-user-auth";
// services
import { IntegrationService } from "@/services/integrations";
@ -33,9 +32,7 @@ const IntegrationGuide = observer(() => {
const router = useRouter();
const { workspaceSlug, provider } = router.query;
// store hooks
const { data: currentUser, isLoading: currentUserLoader, profile } = useUser();
// custom hooks
const {} = useUserAuth({ user: currentUser || null, userProfile: profile?.data, isLoading: currentUserLoader });
const { data: currentUser } = useUser();
const { data: exporterServices } = useSWR(
workspaceSlug && cursor ? EXPORT_SERVICES_LIST(workspaceSlug as string, cursor, `${per_page}`) : null,

View File

@ -20,7 +20,6 @@ import { IMPORTER_SERVICES_LIST } from "@/constants/fetch-keys";
import { IMPORTERS_LIST } from "@/constants/workspace";
// hooks
import { useUser } from "@/hooks/store";
import useUserAuth from "@/hooks/use-user-auth";
// services
import { IntegrationService } from "@/services/integrations";
@ -36,9 +35,7 @@ const IntegrationGuide = observer(() => {
const router = useRouter();
const { workspaceSlug, provider } = router.query;
// store hooks
const { data: currentUser, isLoading: currentUserLoader, profile } = useUser();
// custom hooks
const {} = useUserAuth({ user: currentUser || null, userProfile: profile?.data, isLoading: currentUserLoader });
const { data: currentUser } = useUser();
const { data: importerServices } = useSWR(
workspaceSlug ? IMPORTER_SERVICES_LIST(workspaceSlug as string) : null,

View File

@ -1,108 +0,0 @@
import { useCallback, useState } from "react";
import { useRouter } from "next/router";
// types
import { IUserSettings, IWorkspace, TUserProfile } from "@plane/types";
import { useUserProfile } from "@/hooks/store";
// hooks
import { useWorkspace } from "@/hooks/store";
import { useCurrentUserSettings } from "./store/use-current-user-settings";
type TUseAuthRedirectionProps = {
error: any | null;
isRedirecting: boolean;
handleRedirection: () => Promise<void>;
};
const useAuthRedirection = (): TUseAuthRedirectionProps => {
// states
const [isRedirecting, setIsRedirecting] = useState(false);
const [error, setError] = useState<any | null>(null);
// router
const router = useRouter();
const { next_path } = router.query;
// mobx store
const { fetchUserProfile } = useUserProfile();
const { fetchCurrentUserSettings } = useCurrentUserSettings();
const { fetchWorkspaces } = useWorkspace();
const isValidURL = (url: string): boolean => {
const disallowedSchemes = /^(https?|ftp):\/\//i;
return !disallowedSchemes.test(url);
};
const getAuthRedirectionUrl = useCallback(
async (profile: TUserProfile | undefined) => {
try {
if (!profile) return;
// if the user is not onboarded, redirect them to the onboarding page
if (!profile.is_onboarded) {
return "/onboarding";
}
// if next_path is provided, redirect the user to that url
if (next_path) {
if (isValidURL(next_path.toString())) {
return next_path.toString();
} else {
return "/";
}
}
// Fetch the current user settings
const userSettings: IUserSettings | undefined = await fetchCurrentUserSettings();
const workspacesList: IWorkspace[] = await fetchWorkspaces();
// Extract workspace details
const workspaceSlug =
userSettings?.workspace?.last_workspace_slug || userSettings?.workspace?.fallback_workspace_slug;
// Redirect based on workspace details or to profile if not available
if (
workspaceSlug &&
workspacesList &&
workspacesList.filter((workspace) => workspace.slug === workspaceSlug).length > 0
)
return `/${workspaceSlug}`;
else return "/profile";
} catch (error) {
setIsRedirecting(false);
console.error("Error in handleSignInRedirection:", error);
setError(error);
}
},
[fetchCurrentUserSettings, fetchWorkspaces, router, next_path]
);
const updateUserInfo = useCallback(async () => {
setIsRedirecting(true);
await fetchUserProfile()
.then(async (profile) => {
await getAuthRedirectionUrl(profile)
.then((url: string | undefined) => {
if (url) {
router.push(url);
return;
}
setIsRedirecting(false);
})
.catch((err) => {
setIsRedirecting(false);
setError(err);
});
})
.catch((err) => {
setError(err);
setIsRedirecting(false);
});
}, [fetchUserProfile, getAuthRedirectionUrl]);
return {
error,
isRedirecting,
handleRedirection: updateUserInfo,
};
};
export default useAuthRedirection;

View File

@ -1,11 +1,9 @@
import { useCallback } from "react";
import useSWR from "swr";
// hooks
import useUserAuth from "@/hooks/use-user-auth";
import { IUser, TUserProfile } from "@plane/types";
// services
import { NotificationService } from "@/services/notification.service";
// types
import { IUser, TUserProfile } from "@plane/types";
const userNotificationServices = new NotificationService();
@ -16,8 +14,6 @@ const useUserIssueNotificationSubscription = (
projectId?: string | string[] | null,
issueId?: string | string[] | null
) => {
const {} = useUserAuth({ user: user, userProfile: profile, isLoading: false });
const { data, error, mutate } = useSWR(
workspaceSlug && projectId && issueId ? `SUBSCRIPTION_STATUE_${workspaceSlug}_${projectId}_${issueId}` : null,
workspaceSlug && projectId && issueId

View File

@ -1,114 +0,0 @@
import { useEffect, useState } from "react";
import { useRouter } from "next/router";
// services
import { WorkspaceService } from "@/services/workspace.service";
// types
import { IUser, TUserProfile } from "@plane/types";
const workspaceService = new WorkspaceService();
type Props = {
routeAuth?: "sign-in" | "onboarding" | "admin" | null;
user: IUser | null;
userProfile: TUserProfile | undefined;
isLoading: boolean;
};
const useUserAuth = (props: Props) => {
const { routeAuth, user, userProfile, isLoading } = props;
// states
const [isRouteAccess, setIsRouteAccess] = useState(true);
// router
const router = useRouter();
const { next_path } = router.query;
const isValidURL = (url: string): boolean => {
const disallowedSchemes = /^(https?|ftp):\/\//i;
return !disallowedSchemes.test(url);
};
useEffect(() => {
const handleWorkSpaceRedirection = async () => {
workspaceService.userWorkspaces().then(async (userWorkspaces) => {
if (!user || !userProfile) return;
const firstWorkspace = Object.values(userWorkspaces ?? {})?.[0];
const lastActiveWorkspace = userWorkspaces.find((workspace) => workspace.id === user?.last_workspace_id);
if (lastActiveWorkspace) {
router.push(`/${lastActiveWorkspace.slug}`);
return;
} else if (firstWorkspace) {
router.push(`/${firstWorkspace.slug}`);
return;
} else {
router.push(`/profile`);
return;
}
});
};
const handleUserRouteAuthentication = async () => {
if (user && user.is_active && userProfile) {
if (routeAuth === "sign-in") {
if (userProfile.is_onboarded) handleWorkSpaceRedirection();
else {
router.push("/onboarding");
return;
}
} else if (routeAuth === "onboarding") {
if (userProfile.is_onboarded) handleWorkSpaceRedirection();
else {
setIsRouteAccess(() => false);
return;
}
} else {
if (!userProfile.is_onboarded) {
router.push("/onboarding");
return;
} else {
setIsRouteAccess(() => false);
return;
}
}
} else {
// user is not active and we can redirect to no access page
router.push("/no-access");
// remove token
return;
}
};
if (routeAuth === null) {
setIsRouteAccess(() => false);
return;
} else {
if (!isLoading) {
setIsRouteAccess(() => true);
if (user) {
if (next_path) {
if (isValidURL(next_path.toString())) {
router.push(next_path.toString());
return;
} else {
router.push("/");
return;
}
} else handleUserRouteAuthentication();
} else {
if (routeAuth === "sign-in") {
setIsRouteAccess(() => false);
return;
} else {
router.push("/");
return;
}
}
}
}
}, [user, isLoading, routeAuth, router, next_path]);
return {
isLoading: isRouteAccess,
};
};
export default useUserAuth;

View File

@ -35,7 +35,7 @@ export const AuthenticationWrapper: FC<TAuthenticationWrapper> = observer((props
} = useUser();
const { loader: workspaceLoader, workspaces, fetchWorkspaces } = useWorkspace();
useSWR("USER_INFORMATION", async () => fetchCurrentUser(), {
useSWR("USER_INFORMATION", async () => await fetchCurrentUser(), {
revalidateOnFocus: false,
shouldRetryOnError: false,
});
@ -44,9 +44,9 @@ export const AuthenticationWrapper: FC<TAuthenticationWrapper> = observer((props
currentUser && currentUser?.id ? "USER_PROFILE_SETTINGS_INFORMATION" : null,
async () => {
if (currentUser && currentUser?.id) {
fetchCurrentUserSettings();
fetchUserProfile();
fetchWorkspaces();
await fetchCurrentUserSettings();
await fetchUserProfile();
await fetchWorkspaces();
}
},
{ revalidateOnFocus: false, shouldRetryOnError: false }
@ -82,6 +82,12 @@ export const AuthenticationWrapper: FC<TAuthenticationWrapper> = observer((props
</div>
);
console.log("---");
console.log("currentUser", currentUser?.id);
console.log("currentUserProfile", currentUserProfile?.id);
console.log("currentUserSettings", currentUserSettings?.id);
console.log("---");
if (pageType === EPageTypes.PUBLIC) return <>{children}</>;
if (pageType === EPageTypes.NON_AUTHENTICATED) {

View File

@ -14,7 +14,6 @@ import { USER_ONBOARDING_COMPLETED } from "@/constants/event-tracker";
import { USER_WORKSPACES_LIST } from "@/constants/fetch-keys";
// hooks
import { useUser, useWorkspace, useUserProfile, useEventTracker } from "@/hooks/store";
import useUserAuth from "@/hooks/use-user-auth";
// layouts
import { UserAuthWrapper } from "@/layouts/auth-layout";
import DefaultLayout from "@/layouts/default-layout";
@ -39,16 +38,10 @@ const OnboardingPage: NextPageWithLayout = observer(() => {
const router = useRouter();
// store hooks
const { captureEvent } = useEventTracker();
const { data: user, isLoading: currentUserLoader, updateCurrentUser } = useUser();
const { data: user, updateCurrentUser } = useUser();
const { data: profile, updateUserOnBoard, updateUserProfile } = useUserProfile();
const { workspaces, fetchWorkspaces } = useWorkspace();
// custom hooks
const {} = useUserAuth({
routeAuth: "onboarding",
user: user || null,
userProfile: profile,
isLoading: currentUserLoader,
});
// computed values
const workspacesList = Object.values(workspaces ?? {});
// fetching workspaces list

View File

@ -29,7 +29,6 @@ import { TIME_ZONES } from "@/constants/timezones";
import { USER_ROLES } from "@/constants/workspace";
// hooks
import { useAppTheme, useUser } from "@/hooks/store";
import useUserAuth from "@/hooks/use-user-auth";
import { ProfileSettingsLayout } from "@/layouts/settings-layout";
// layouts
// lib types
@ -66,14 +65,8 @@ const ProfileSettingsPage: NextPageWithLayout = observer(() => {
formState: { errors },
} = useForm<IUser>({ defaultValues });
// store hooks
const {
data: currentUser,
updateCurrentUser,
profile: { isLoading: currentUserLoader, data },
} = useUser();
const { data: currentUser, updateCurrentUser } = useUser();
const { toggleSidebar } = useAppTheme();
// custom hooks
const {} = useUserAuth({ user: currentUser || null, userProfile: data, isLoading: currentUserLoader });
useEffect(() => {
reset({ ...defaultValues, ...currentUser });

View File

@ -35,7 +35,7 @@ export interface IUserStore {
export class UserStore implements IUserStore {
// observables flags
isAuthenticated: boolean = false;
isLoading: boolean = false;
isLoading: boolean = true;
error: any | undefined = undefined;
data: IUser | undefined = undefined;
// model observables

View File

@ -22,7 +22,7 @@ export interface IProfileStore {
}
export class ProfileStore implements IProfileStore {
isLoading: boolean = false;
isLoading: boolean = true;
data: TUserProfile = {
id: undefined,
user: undefined,

View File

@ -19,7 +19,7 @@ export interface IUserSettingsStore {
}
export class UserSettingsStore implements IUserSettingsStore {
isLoading: boolean = false;
isLoading: boolean = true;
data: IUserSettings = {
id: undefined,
email: undefined,