New Directory Setup (#2065)
* chore: moved app & space from apps to root * chore: modified workspace configuration * chore: modified dockerfiles for space and web * chore: modified icons for space * feat: updated files for new svg icons supported by next-images * chore: added /spaces base path for next * chore: added compose config for space * chore: updated husky configuration * chore: updated workflows for new configuration * chore: changed app name to web * fix: resolved build errors with web * chore: reset file tracing root for both projects * chore: added nginx config for deploy * fix: eslint and tsconfig settings for space app * husky setup fixes based on new dir * eslint fixes * prettier formatting --------- Co-authored-by: Henit Chobisa <chobisa.henit@gmail.com>
This commit is contained in:
parent
20e36194b4
commit
1e152c666c
1022 changed files with 1475 additions and 1240 deletions
117
web/hooks/use-user-auth.tsx
Normal file
117
web/hooks/use-user-auth.tsx
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
import { useEffect, useState } from "react";
|
||||
// next imports
|
||||
import { useRouter } from "next/router";
|
||||
// swr
|
||||
import useSWR from "swr";
|
||||
// keys
|
||||
import { CURRENT_USER } from "constants/fetch-keys";
|
||||
// services
|
||||
import userService from "services/user.service";
|
||||
import workspaceService from "services/workspace.service";
|
||||
// types
|
||||
import type { IWorkspace, ICurrentUserResponse } from "types";
|
||||
|
||||
const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "admin") => {
|
||||
const router = useRouter();
|
||||
const { next_url } = router.query as { next_url: string };
|
||||
|
||||
const [isRouteAccess, setIsRouteAccess] = useState(true);
|
||||
|
||||
const {
|
||||
data: user,
|
||||
isLoading,
|
||||
error,
|
||||
mutate,
|
||||
} = useSWR<ICurrentUserResponse>(CURRENT_USER, () => userService.currentUser(), {
|
||||
refreshInterval: 0,
|
||||
shouldRetryOnError: false,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const handleWorkSpaceRedirection = async () => {
|
||||
workspaceService.userWorkspaces().then(async (userWorkspaces) => {
|
||||
const lastActiveWorkspace = userWorkspaces.find(
|
||||
(workspace: IWorkspace) => workspace.id === user?.last_workspace_id
|
||||
);
|
||||
if (lastActiveWorkspace) {
|
||||
router.push(`/${lastActiveWorkspace.slug}`);
|
||||
return;
|
||||
} else if (userWorkspaces.length > 0) {
|
||||
router.push(`/${userWorkspaces[0].slug}`);
|
||||
return;
|
||||
} else {
|
||||
const invitations = await workspaceService.userWorkspaceInvitations();
|
||||
if (invitations.length > 0) {
|
||||
router.push(`/invitations`);
|
||||
return;
|
||||
} else {
|
||||
router.push(`/create-workspace`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
if (routeAuth === null) {
|
||||
setIsRouteAccess(() => false);
|
||||
return;
|
||||
} else {
|
||||
if (!isLoading) {
|
||||
setIsRouteAccess(() => true);
|
||||
if (user) {
|
||||
if (next_url) router.push(next_url);
|
||||
else handleUserRouteAuthentication();
|
||||
} else {
|
||||
if (routeAuth === "sign-in") {
|
||||
setIsRouteAccess(() => false);
|
||||
return;
|
||||
} else {
|
||||
router.push("/");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [user, isLoading, routeAuth, router, next_url]);
|
||||
|
||||
return {
|
||||
isLoading: isRouteAccess,
|
||||
user: error ? undefined : user,
|
||||
mutateUser: mutate,
|
||||
assignedIssuesLength: user?.assigned_issues ?? 0,
|
||||
workspaceInvitesLength: user?.workspace_invites ?? 0,
|
||||
};
|
||||
};
|
||||
|
||||
export default useUserAuth;
|
||||
Loading…
Add table
Add a link
Reference in a new issue