Skip to content

Commit

Permalink
Merge pull request #4200 from systeminit/redirect-to-default-workspace
Browse files Browse the repository at this point in the history
feat(auth-portal): Redirect to the default workspace on login
  • Loading branch information
stack72 authored Jul 23, 2024
2 parents b9b108e + 29b85e0 commit f4d339d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
29 changes: 29 additions & 0 deletions app/auth-portal/src/pages/DefaultWorkspacePage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<template><div>Redirect to default workspace</div></template>

<script setup lang="ts">
import { computed, onMounted } from "vue";
import { useRouter } from "vue-router";
import { useWorkspacesStore } from "@/store/workspaces.store";
import { useAuthStore } from "@/store/auth.store";
import { API_HTTP_URL } from "@/store/api";
const authStore = useAuthStore();
const router = useRouter();
const workspacesStore = useWorkspacesStore();
const defaultWorkspace = computed(() => workspacesStore.defaultWorkspace);
onMounted(async () => {
if (import.meta.env.SSR) return;
if (!authStore.userIsLoggedIn) return;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
await workspacesStore.LOAD_WORKSPACES();
if (defaultWorkspace.value) {
window.location.href = `${API_HTTP_URL}/workspaces/${defaultWorkspace.value.id}/go`;
} else {
await router.push({
name: "workspaces",
});
}
});
</script>
2 changes: 1 addition & 1 deletion app/auth-portal/src/pages/ProfilePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion app/auth-portal/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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",
Expand All @@ -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 },
Expand Down
29 changes: 23 additions & 6 deletions app/auth-portal/src/store/workspaces.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit f4d339d

Please sign in to comment.