From 14045dbcc5567be71a80bfbc7e0d59ed7a6f1131 Mon Sep 17 00:00:00 2001 From: dakshesh14 Date: Tue, 3 Oct 2023 15:56:44 +0530 Subject: [PATCH 1/3] fix: 404 when redirecting user to login page --- space/pages/index.tsx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 space/pages/index.tsx diff --git a/space/pages/index.tsx b/space/pages/index.tsx new file mode 100644 index 00000000000..6e9b17ba6e5 --- /dev/null +++ b/space/pages/index.tsx @@ -0,0 +1,19 @@ +import { useEffect } from "react"; + +// next +import { NextPage } from "next"; +import { useRouter } from "next/router"; + +const Index: NextPage = () => { + const router = useRouter(); + // using asPath instead of query because query can be undefined + const nextPath = router.asPath.split("next_path=")[1]; + + useEffect(() => { + router.push(`/login?next_path=${nextPath}`); + }, [nextPath, router]); + + return null; +}; + +export default Index; From b0f799c793150c09164fe8d8b6c443ee3c1e7248 Mon Sep 17 00:00:00 2001 From: dakshesh14 Date: Wed, 4 Oct 2023 18:08:50 +0530 Subject: [PATCH 2/3] fix: next_path redirection not working --- space/components/accounts/sign-in.tsx | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/space/components/accounts/sign-in.tsx b/space/components/accounts/sign-in.tsx index c6a151d4423..ad22efd1f6c 100644 --- a/space/components/accounts/sign-in.tsx +++ b/space/components/accounts/sign-in.tsx @@ -19,6 +19,7 @@ export const SignInView = observer(() => { const { user: userStore } = useMobxStore(); const router = useRouter(); + const nextPath = router.asPath.split("?next_path=")[1]; const { setToastAlert } = useToast(); @@ -33,15 +34,11 @@ export const SignInView = observer(() => { const onSignInSuccess = (response: any) => { const isOnboarded = response?.user?.onboarding_step?.profile_complete || false; - const nextPath = router.asPath.includes("next_path") ? router.asPath.split("/?next_path=")[1] : "/login"; - userStore.setCurrentUser(response?.user); if (!isOnboarded) { router.push(`/onboarding?next_path=${nextPath}`); - return; - } - router.push((nextPath ?? "/login").toString()); + } else router.push(nextPath ?? "/login"); }; const handleGoogleSignIn = async ({ clientId, credential }: any) => { From c4725224acad0cd1881035fd94c597e1384474fa Mon Sep 17 00:00:00 2001 From: gurusainath Date: Wed, 4 Oct 2023 19:11:35 +0530 Subject: [PATCH 3/3] fix: authentication workflow update in plane deploy --- space/components/accounts/sign-in.tsx | 16 ++++++++++------ space/components/issues/navbar/index.tsx | 2 +- space/components/views/login.tsx | 12 +++++++++--- space/lib/mobx/store-init.tsx | 9 ++++++++- space/pages/index.tsx | 8 ++++---- space/pages/login/index.tsx | 2 +- space/store/user.ts | 10 ++++++++++ 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/space/components/accounts/sign-in.tsx b/space/components/accounts/sign-in.tsx index ad22efd1f6c..8f6d1c5f14a 100644 --- a/space/components/accounts/sign-in.tsx +++ b/space/components/accounts/sign-in.tsx @@ -19,7 +19,7 @@ export const SignInView = observer(() => { const { user: userStore } = useMobxStore(); const router = useRouter(); - const nextPath = router.asPath.split("?next_path=")[1]; + const { next_path } = router.query as { next_path: string }; const { setToastAlert } = useToast(); @@ -32,13 +32,17 @@ export const SignInView = observer(() => { }; const onSignInSuccess = (response: any) => { - const isOnboarded = response?.user?.onboarding_step?.profile_complete || false; - userStore.setCurrentUser(response?.user); - if (!isOnboarded) { - router.push(`/onboarding?next_path=${nextPath}`); - } else router.push(nextPath ?? "/login"); + const isOnboard = response?.user?.onboarding_step?.profile_complete || false; + + if (isOnboard) { + if (next_path) router.push(next_path); + else router.push("/login"); + } else { + if (next_path) router.push(`/onboarding?next_path=${next_path}`); + else router.push("/onboarding"); + } }; const handleGoogleSignIn = async ({ clientId, credential }: any) => { diff --git a/space/components/issues/navbar/index.tsx b/space/components/issues/navbar/index.tsx index 35ffbe289c2..03f082f33a3 100644 --- a/space/components/issues/navbar/index.tsx +++ b/space/components/issues/navbar/index.tsx @@ -129,7 +129,7 @@ const IssueNavbar = observer(() => { ) : (
- + Sign in diff --git a/space/components/views/login.tsx b/space/components/views/login.tsx index d01a226811f..406d6be9877 100644 --- a/space/components/views/login.tsx +++ b/space/components/views/login.tsx @@ -7,7 +7,13 @@ import { SignInView, UserLoggedIn } from "components/accounts"; export const LoginView = observer(() => { const { user: userStore } = useMobxStore(); - if (!userStore.currentUser) return ; - - return ; + return ( + <> + {userStore?.loader ? ( +
Loading
+ ) : ( + <>{userStore.currentUser ? : } + )} + + ); }); diff --git a/space/lib/mobx/store-init.tsx b/space/lib/mobx/store-init.tsx index 6e38d9c6d17..4fc761ad194 100644 --- a/space/lib/mobx/store-init.tsx +++ b/space/lib/mobx/store-init.tsx @@ -3,12 +3,14 @@ import { useEffect } from "react"; // next imports import { useRouter } from "next/router"; +// js cookie +import Cookie from "js-cookie"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; import { RootStore } from "store/root"; const MobxStoreInit = () => { - const store: RootStore = useMobxStore(); + const { user: userStore }: RootStore = useMobxStore(); const router = useRouter(); const { states, labels, priorities } = router.query as { states: string[]; labels: string[]; priorities: string[] }; @@ -19,6 +21,11 @@ const MobxStoreInit = () => { // store.issue.userSelectedStates = states || []; // }, [store.issue]); + useEffect(() => { + const authToken = Cookie.get("accessToken") || null; + if (authToken) userStore.fetchCurrentUser(); + }, [userStore]); + return <>; }; diff --git a/space/pages/index.tsx b/space/pages/index.tsx index 6e9b17ba6e5..1ff239253ac 100644 --- a/space/pages/index.tsx +++ b/space/pages/index.tsx @@ -6,12 +6,12 @@ import { useRouter } from "next/router"; const Index: NextPage = () => { const router = useRouter(); - // using asPath instead of query because query can be undefined - const nextPath = router.asPath.split("next_path=")[1]; + const { next_path } = router.query as { next_path: string }; useEffect(() => { - router.push(`/login?next_path=${nextPath}`); - }, [nextPath, router]); + if (next_path) router.push(`/login?next_path=${next_path}`); + else router.push(`/login`); + }, [router, next_path]); return null; }; diff --git a/space/pages/login/index.tsx b/space/pages/login/index.tsx index a80eff8736d..9f20f099f4a 100644 --- a/space/pages/login/index.tsx +++ b/space/pages/login/index.tsx @@ -5,4 +5,4 @@ import { LoginView } from "components/views"; const LoginPage = () => ; -export default LoginPage; \ No newline at end of file +export default LoginPage; diff --git a/space/store/user.ts b/space/store/user.ts index 3a76c211177..cc7334e872e 100644 --- a/space/store/user.ts +++ b/space/store/user.ts @@ -7,12 +7,17 @@ import { ActorDetail } from "types/issue"; import { IUser } from "types/user"; export interface IUserStore { + loader: boolean; + error: any | null; currentUser: any | null; fetchCurrentUser: () => void; currentActor: () => any; } class UserStore implements IUserStore { + loader: boolean = false; + error: any | null = null; + currentUser: IUser | null = null; // root store rootStore; @@ -73,14 +78,19 @@ class UserStore implements IUserStore { fetchCurrentUser = async () => { try { + this.loader = true; + this.error = null; const response = await this.userService.currentUser(); if (response) { runInAction(() => { + this.loader = false; this.currentUser = response; }); } } catch (error) { console.error("Failed to fetch current user", error); + this.loader = false; + this.error = error; } }; }