mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
fix: user authentication on the index page (#2619)
* fix: user authentication on the index page * fix: login redirection cleanup --------- Co-authored-by: sriram veeraghanta <veeraghanta.sriram@gmail.com>
This commit is contained in:
parent
5efc6993cd
commit
260974b0de
@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useCallback } from "react";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import { observer } from "mobx-react-lite";
|
import { observer } from "mobx-react-lite";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
@ -22,17 +22,18 @@ 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";
|
||||||
// types
|
// types
|
||||||
import { IUserSettings } from "types";
|
import { IUser, IUserSettings } from "types";
|
||||||
|
|
||||||
const appConfigService = new AppConfigService();
|
const appConfigService = new AppConfigService();
|
||||||
const authService = new AuthService();
|
const authService = new AuthService();
|
||||||
|
|
||||||
export const SignInView = observer(() => {
|
export const SignInView = observer(() => {
|
||||||
const { user: userStore } = useMobxStore();
|
const {
|
||||||
|
user: { fetchCurrentUser, fetchCurrentUserSettings },
|
||||||
|
} = useMobxStore();
|
||||||
// router
|
// router
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { next: next_url } = router.query as { next: string };
|
const { next: next_url } = router.query as { next: string };
|
||||||
|
|
||||||
// states
|
// states
|
||||||
const [isLoading, setLoading] = useState(false);
|
const [isLoading, setLoading] = useState(false);
|
||||||
// toast
|
// toast
|
||||||
@ -44,44 +45,44 @@ export const SignInView = observer(() => {
|
|||||||
data &&
|
data &&
|
||||||
(data?.email_password_login || !(data?.email_password_login || data?.magic_login || data?.google || data?.github));
|
(data?.email_password_login || !(data?.email_password_login || data?.magic_login || data?.google || data?.github));
|
||||||
|
|
||||||
useEffect(() => {
|
const handleLoginRedirection = useCallback(
|
||||||
userStore.fetchCurrentUserSettings().then((settings) => {
|
(user: IUser) => {
|
||||||
setLoading(true);
|
// if the user is not onboarded, redirect them to the onboarding page
|
||||||
if (next_url) router.push(next_url);
|
if (!user.is_onboarded) {
|
||||||
else
|
|
||||||
router.push(
|
|
||||||
`/${
|
|
||||||
settings.workspace.last_workspace_slug
|
|
||||||
? settings.workspace.last_workspace_slug
|
|
||||||
: settings.workspace.fallback_workspace_slug
|
|
||||||
}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}, [userStore, router, next_url]);
|
|
||||||
|
|
||||||
const handleLoginRedirection = () => {
|
|
||||||
userStore.fetchCurrentUser().then((user) => {
|
|
||||||
const isOnboarded = user.is_onboarded;
|
|
||||||
|
|
||||||
if (isOnboarded) {
|
|
||||||
userStore
|
|
||||||
.fetchCurrentUserSettings()
|
|
||||||
.then((userSettings: IUserSettings) => {
|
|
||||||
const workspaceSlug =
|
|
||||||
userSettings?.workspace?.last_workspace_slug || userSettings?.workspace?.fallback_workspace_slug;
|
|
||||||
if (next_url) router.push(next_url);
|
|
||||||
else if (workspaceSlug) router.push(`/${workspaceSlug}`);
|
|
||||||
else if (userSettings.workspace.invites > 0) router.push("/invitations");
|
|
||||||
else router.push("/create-workspace");
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
setLoading(false);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
router.push("/onboarding");
|
router.push("/onboarding");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
// if next_url is provided, redirect the user to that url
|
||||||
|
if (next_url) {
|
||||||
|
router.push(next_url);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the user is onboarded, fetch their last workspace details
|
||||||
|
fetchCurrentUserSettings()
|
||||||
|
.then((userSettings: IUserSettings) => {
|
||||||
|
const workspaceSlug =
|
||||||
|
userSettings?.workspace?.last_workspace_slug || userSettings?.workspace?.fallback_workspace_slug;
|
||||||
|
if (workspaceSlug) router.push(`/${workspaceSlug}`);
|
||||||
|
else if (userSettings.workspace.invites > 0) router.push("/invitations");
|
||||||
|
else router.push("/create-workspace");
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[fetchCurrentUserSettings, router, next_url]
|
||||||
|
);
|
||||||
|
|
||||||
|
const mutateUserInfo = useCallback(() => {
|
||||||
|
fetchCurrentUser().then((user) => {
|
||||||
|
handleLoginRedirection(user);
|
||||||
});
|
});
|
||||||
};
|
}, [fetchCurrentUser, handleLoginRedirection]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
mutateUserInfo();
|
||||||
|
}, [mutateUserInfo]);
|
||||||
|
|
||||||
const handleGoogleSignIn = async ({ clientId, credential }: any) => {
|
const handleGoogleSignIn = async ({ clientId, credential }: any) => {
|
||||||
try {
|
try {
|
||||||
@ -94,7 +95,7 @@ export const SignInView = observer(() => {
|
|||||||
};
|
};
|
||||||
const response = await authService.socialAuth(socialAuthPayload);
|
const response = await authService.socialAuth(socialAuthPayload);
|
||||||
if (response) {
|
if (response) {
|
||||||
handleLoginRedirection();
|
mutateUserInfo();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
@ -121,7 +122,7 @@ export const SignInView = observer(() => {
|
|||||||
};
|
};
|
||||||
const response = await authService.socialAuth(socialAuthPayload);
|
const response = await authService.socialAuth(socialAuthPayload);
|
||||||
if (response) {
|
if (response) {
|
||||||
handleLoginRedirection();
|
mutateUserInfo();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
@ -142,13 +143,7 @@ export const SignInView = observer(() => {
|
|||||||
return authService
|
return authService
|
||||||
.emailLogin(formData)
|
.emailLogin(formData)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
userStore.fetchCurrentUser().then((user) => {
|
mutateUserInfo();
|
||||||
const isOnboard = user.onboarding_step.profile_complete;
|
|
||||||
if (isOnboard) handleLoginRedirection();
|
|
||||||
else {
|
|
||||||
router.push("/onboarding");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
@ -164,7 +159,7 @@ export const SignInView = observer(() => {
|
|||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
if (response) {
|
if (response) {
|
||||||
handleLoginRedirection();
|
mutateUserInfo();
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@ -3,7 +3,6 @@ import { useRouter } from "next/router";
|
|||||||
import { observer } from "mobx-react-lite";
|
import { observer } from "mobx-react-lite";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { Menu, Transition } from "@headlessui/react";
|
import { Menu, Transition } from "@headlessui/react";
|
||||||
import { useTheme } from "next-themes";
|
|
||||||
import { Check, LogOut, Plus, Settings, UserCircle2 } from "lucide-react";
|
import { Check, LogOut, Plus, Settings, UserCircle2 } from "lucide-react";
|
||||||
// mobx store
|
// mobx store
|
||||||
import { useMobxStore } from "lib/mobx/store-provider";
|
import { useMobxStore } from "lib/mobx/store-provider";
|
||||||
@ -55,8 +54,6 @@ export const WorkspaceSidebarDropdown = observer(() => {
|
|||||||
const { workspaces, currentWorkspace: activeWorkspace } = workspaceStore;
|
const { workspaces, currentWorkspace: activeWorkspace } = workspaceStore;
|
||||||
const user = userStore.currentUser;
|
const user = userStore.currentUser;
|
||||||
|
|
||||||
const { setTheme } = useTheme();
|
|
||||||
|
|
||||||
const { setToastAlert } = useToast();
|
const { setToastAlert } = useToast();
|
||||||
|
|
||||||
const handleWorkspaceNavigation = (workspace: IWorkspace) => {
|
const handleWorkspaceNavigation = (workspace: IWorkspace) => {
|
||||||
@ -81,7 +78,6 @@ export const WorkspaceSidebarDropdown = observer(() => {
|
|||||||
.signOut()
|
.signOut()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
router.push("/");
|
router.push("/");
|
||||||
setTheme("system");
|
|
||||||
})
|
})
|
||||||
.catch(() =>
|
.catch(() =>
|
||||||
setToastAlert({
|
setToastAlert({
|
||||||
|
@ -9,7 +9,7 @@ import { CURRENT_USER } from "constants/fetch-keys";
|
|||||||
import { UserService } from "services/user.service";
|
import { UserService } from "services/user.service";
|
||||||
import { WorkspaceService } from "services/workspace.service";
|
import { WorkspaceService } from "services/workspace.service";
|
||||||
// types
|
// types
|
||||||
import type { IWorkspace, IUser } from "types";
|
import type { IUser } from "types";
|
||||||
|
|
||||||
const userService = new UserService();
|
const userService = new UserService();
|
||||||
const workspaceService = new WorkspaceService();
|
const workspaceService = new WorkspaceService();
|
||||||
@ -33,9 +33,7 @@ const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "adm
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleWorkSpaceRedirection = async () => {
|
const handleWorkSpaceRedirection = async () => {
|
||||||
workspaceService.userWorkspaces().then(async (userWorkspaces) => {
|
workspaceService.userWorkspaces().then(async (userWorkspaces) => {
|
||||||
const lastActiveWorkspace = userWorkspaces.find(
|
const lastActiveWorkspace = userWorkspaces.find((workspace) => workspace.id === user?.last_workspace_id);
|
||||||
(workspace: IWorkspace) => workspace.id === user?.last_workspace_id
|
|
||||||
);
|
|
||||||
if (lastActiveWorkspace) {
|
if (lastActiveWorkspace) {
|
||||||
router.push(`/${lastActiveWorkspace.slug}`);
|
router.push(`/${lastActiveWorkspace.slug}`);
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user