2023-05-30 13:44:35 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
// next imports
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
// swr
|
|
|
|
import useSWR from "swr";
|
|
|
|
// services
|
2023-12-04 06:41:36 +00:00
|
|
|
import { WorkspaceService } from "services/workspace.service";
|
|
|
|
// mobx
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
|
|
|
|
|
|
|
const workspaceService = new WorkspaceService();
|
2023-05-30 13:44:35 +00:00
|
|
|
|
|
|
|
const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "admin") => {
|
|
|
|
const router = useRouter();
|
2023-12-04 06:41:36 +00:00
|
|
|
const { next_url } = router.query;
|
2023-06-05 12:18:29 +00:00
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
const [isRouteAccess, setIsRouteAccess] = useState(true);
|
2023-12-04 06:41:36 +00:00
|
|
|
const {
|
|
|
|
user: { fetchCurrentUser },
|
|
|
|
} = useMobxStore();
|
2023-05-30 13:44:35 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
data: user,
|
|
|
|
isLoading,
|
|
|
|
error,
|
|
|
|
mutate,
|
2023-12-04 06:41:36 +00:00
|
|
|
} = useSWR("CURRENT_USER_DETAILS", () => fetchCurrentUser(), {
|
2023-06-07 08:17:56 +00:00
|
|
|
refreshInterval: 0,
|
2023-06-09 19:56:38 +00:00
|
|
|
shouldRetryOnError: false,
|
2023-06-07 08:17:56 +00:00
|
|
|
});
|
2023-05-30 13:44:35 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleWorkSpaceRedirection = async () => {
|
|
|
|
workspaceService.userWorkspaces().then(async (userWorkspaces) => {
|
2023-12-04 06:41:36 +00:00
|
|
|
const lastActiveWorkspace = userWorkspaces.find((workspace) => workspace.id === user?.last_workspace_id);
|
2023-05-30 13:44:35 +00:00
|
|
|
if (lastActiveWorkspace) {
|
|
|
|
router.push(`/${lastActiveWorkspace.slug}`);
|
|
|
|
return;
|
|
|
|
} else if (userWorkspaces.length > 0) {
|
|
|
|
router.push(`/${userWorkspaces[0].slug}`);
|
|
|
|
return;
|
|
|
|
} else {
|
2023-12-04 06:41:36 +00:00
|
|
|
router.push(`/profile`);
|
|
|
|
return;
|
2023-05-30 13:44:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleUserRouteAuthentication = async () => {
|
|
|
|
if (user && user.is_active) {
|
|
|
|
if (routeAuth === "sign-in") {
|
|
|
|
if (user.is_onboarded) handleWorkSpaceRedirection();
|
|
|
|
else {
|
|
|
|
router.push("/onboarding");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (routeAuth === "onboarding") {
|
|
|
|
if (user.is_onboarded) handleWorkSpaceRedirection();
|
|
|
|
else {
|
|
|
|
setIsRouteAccess(() => false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!user.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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-07 08:17:56 +00:00
|
|
|
if (routeAuth === null) {
|
|
|
|
setIsRouteAccess(() => false);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!isLoading) {
|
|
|
|
setIsRouteAccess(() => true);
|
|
|
|
if (user) {
|
2023-12-04 06:41:36 +00:00
|
|
|
if (next_url) router.push(next_url.toString());
|
2023-06-07 08:17:56 +00:00
|
|
|
else handleUserRouteAuthentication();
|
2023-05-30 13:44:35 +00:00
|
|
|
} else {
|
2023-06-07 08:17:56 +00:00
|
|
|
if (routeAuth === "sign-in") {
|
|
|
|
setIsRouteAccess(() => false);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
router.push("/");
|
|
|
|
return;
|
|
|
|
}
|
2023-05-30 13:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 12:18:29 +00:00
|
|
|
}, [user, isLoading, routeAuth, router, next_url]);
|
2023-05-30 13:44:35 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
isLoading: isRouteAccess,
|
|
|
|
user: error ? undefined : user,
|
|
|
|
mutateUser: mutate,
|
2023-12-04 06:41:36 +00:00
|
|
|
// assignedIssuesLength: user?.assigned_issues ?? 0,
|
|
|
|
// workspaceInvitesLength: user?.workspace_invites ?? 0,
|
2023-05-30 13:44:35 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useUserAuth;
|