From 29b85e0986f6a51d11cc5f108316bbdae5366583 Mon Sep 17 00:00:00 2001 From: stack72 Date: Tue, 23 Jul 2024 20:28:44 +0100 Subject: [PATCH] feat(auth-portal): Redirect to the default workspace on login When a user logs into the app (not from a redirect), we want to take the user to their default workspace. The default workspace is chosen in the following order: * If the workspace has a flag of isDefault * The first production workspace owned by the user This means that by shipping this code, we only affect users who have a production workspace. This will not redirect users with a local workspace --- .../src/pages/DefaultWorkspacePage.vue | 29 +++++++++++++++++++ app/auth-portal/src/pages/ProfilePage.vue | 2 +- app/auth-portal/src/router.ts | 8 ++++- app/auth-portal/src/store/workspaces.store.ts | 29 +++++++++++++++---- 4 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 app/auth-portal/src/pages/DefaultWorkspacePage.vue diff --git a/app/auth-portal/src/pages/DefaultWorkspacePage.vue b/app/auth-portal/src/pages/DefaultWorkspacePage.vue new file mode 100644 index 0000000000..f8886d7646 --- /dev/null +++ b/app/auth-portal/src/pages/DefaultWorkspacePage.vue @@ -0,0 +1,29 @@ + + + diff --git a/app/auth-portal/src/pages/ProfilePage.vue b/app/auth-portal/src/pages/ProfilePage.vue index 0429bdb472..e0948c31a7 100644 --- a/app/auth-portal/src/pages/ProfilePage.vue +++ b/app/auth-portal/src/pages/ProfilePage.vue @@ -230,7 +230,7 @@ const saveHandler = async () => { const completeProfileReq = await authStore.COMPLETE_PROFILE({}); if (completeProfileReq.result.success) { if (featureFlagsStore.SIMPLIFIED_SIGNUP) { - if (authStore.user?.emailVerified) { + if (authStore.user?.emailVerified && workspacesStore.defaultWorkspace) { tracker.trackEvent("workspace_launcher_widget_click"); window.location.href = `${API_HTTP_URL}/workspaces/${workspacesStore.defaultWorkspace.id}/go`; } else { diff --git a/app/auth-portal/src/router.ts b/app/auth-portal/src/router.ts index 7aed77606c..75bc3e7d74 100644 --- a/app/auth-portal/src/router.ts +++ b/app/auth-portal/src/router.ts @@ -11,6 +11,7 @@ import LogoutSuccessPage from "./pages/LogoutSuccessPage.vue"; import NotFoundPage from "./pages/NotFoundPage.vue"; import WorkspacesPage from "./pages/WorkspacesPage.vue"; import ProfilePage from "./pages/ProfilePage.vue"; +import DefaultWorkspacePage from "./pages/DefaultWorkspacePage.vue"; // normally we'd initialze a router directly, but instead we pass the options to ViteSSG export const routerOptions: RouterOptions = { @@ -58,6 +59,11 @@ export const routerOptions: RouterOptions = { }, }, { path: "/workspaces", name: "workspaces", component: WorkspacesPage }, + { + path: "/default_workspace", + name: "default-workspace", + component: DefaultWorkspacePage, + }, { path: "/workspace/:workspaceId", name: "workspace-settings", @@ -77,7 +83,7 @@ export const routerOptions: RouterOptions = { // see App.vue for logic saving this redirect location const savedPath = storage.getItem("SI-LOGIN-REDIRECT"); storage.removeItem("SI-LOGIN-REDIRECT"); - return savedPath || { name: "workspaces" }; + return savedPath || { name: "default-workspace" }; }, }, { path: "/:catchAll(.*)", name: "404", component: NotFoundPage }, diff --git a/app/auth-portal/src/store/workspaces.store.ts b/app/auth-portal/src/store/workspaces.store.ts index 21d1c29a5c..f2123ee102 100644 --- a/app/auth-portal/src/store/workspaces.store.ts +++ b/app/auth-portal/src/store/workspaces.store.ts @@ -44,13 +44,30 @@ export const useWorkspacesStore = defineStore("workspaces", { // grabbing the oldest workspace you created and assuming that it's your "default" defaultWorkspace: (state) => { const authStore = useAuthStore(); - return _.sortBy( - _.filter( - _.values(state.workspacesById), - (w) => w.creatorUserId === authStore.user?.id, + + // Let's first check for a defaultWorkspace + const defaultWorkspace = _.head( + _.filter(_.values(state.workspacesById), (w) => w.isDefault), + ); + if (defaultWorkspace) return defaultWorkspace; + + // There's no direct defaultWorkspace so get the first created production workspace for that user + const firstProductionWorkspace = _.head( + _.sortBy( + _.filter( + _.values(state.workspacesById), + (w) => + w.creatorUserId === authStore.user?.id && + w.instanceEnvType === "SI", + ), + (w) => w.createdAt, ), - (w) => w.createdAt, - )[0]; + ); + if (firstProductionWorkspace) return firstProductionWorkspace; + + // This user has no production workspaces so we should not + // redirect them anywhere but the workspaces page... for now! + return null; }, }, actions: {