ubiquitous-invention/apps/web/app/page.tsx
Randall Stillwell 7ec2ede7ca feat(web): land authed users on their oldest workspace home by slug
Path-A task 4/5. The root landing logic in apps/web/app/page.tsx was
redirecting to `/${workspaceId}` (UUID, ugly) and using no ORDER BY
(so two sessions could land on different workspaces). It also looped
zero-workspace users through `/sign-in`.

Changes:

* Inner-join workspaceMembers with workspaces to fetch the slug, not
  just the id. Order by membership createdAt ascending so users
  consistently hit their oldest workspace.
* Redirect to /{slug} (slug, not UUID).
* Removed the unused `objects` / `and` imports that were lint
  warnings.
* Zero-workspace branch redirects to /sign-in?error=no_workspace as a
  defensive fallback; documented inline that this is unreachable for
  fresh sign-ins post `ensureUserHasWorkspace` in apps/web/lib/auth.ts.

The dashboard at /{slug}/ is no longer a mockup (post commit f64d307
which wired it to objects.stats and objects.listRecent), so landing
there now shows real state.

Filed plans/Plan-daily-driver-finish/Epic-shipping-the-shell/
Task-onboarding-zero-workspace-flow.md (P2) as the follow-up that
turns the defensive fallback into a proper welcome flow with a
shared workspace-provisioning helper.

`pnpm lint && pnpm type-check` clean. Closes
plans/Plan-daily-driver-finish/Epic-shipping-the-shell/
Task-pick-workspace-landing-route.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-02 00:25:48 -05:00

51 lines
2.1 KiB
TypeScript

import { redirect } from "next/navigation";
import { asc, eq } from "drizzle-orm";
import { auth } from "@/lib/auth";
import { db } from "@tasks/database/client";
import { workspaceMembers, workspaces } from "@tasks/database/schema";
/**
* Root landing route. Decides where an authenticated user goes when they
* hit `/`. The post-sign-in `callbackUrl` defaults here, so this is the
* single source of truth for "where does a user actually land?"
*
* Decision: land on the user's oldest workspace home (`/{slug}/`). Why oldest:
* we don't have a "last visited" column yet (deliberately deferred — see
* Task-pick-workspace-landing-route) and oldest-membership is stable across
* page loads. The workspace home is no longer a mockup; it now shows real
* stats and recent activity via `objects.stats` / `objects.listRecent`.
*
* The auth callback (`ensureUserHasWorkspace` in `apps/web/lib/auth.ts`)
* provisions a personal workspace on first sign-in, so the zero-workspace
* branch below should be effectively unreachable for fresh sign-ins. It
* survives only as a defensive fallback for sessions that were minted
* before that provisioning logic existed.
*/
export default async function HomePage() {
const session = await auth();
if (!session?.user?.id) {
redirect("/sign-in");
}
const [membership] = await db
.select({ slug: workspaces.slug })
.from(workspaceMembers)
.innerJoin(workspaces, eq(workspaces.id, workspaceMembers.workspaceId))
.where(eq(workspaceMembers.userId, session.user.id))
.orderBy(asc(workspaceMembers.createdAt))
.limit(1);
if (membership?.slug) {
redirect(`/${membership.slug}`);
}
// Zero-workspace fallback. See header doc — this should be unreachable
// post-`ensureUserHasWorkspace`. If we hit it anyway, bounce through
// sign-in so the auth callback re-runs and provisions a workspace.
// Track follow-up: a proper onboarding flow for users in this state
// is captured in Plan-daily-driver-finish/Epic-shipping-the-shell/
// Task-onboarding-zero-workspace-flow.md.
redirect("/sign-in?error=no_workspace");
}