diff --git a/shared/components/rstack-space-switcher.ts b/shared/components/rstack-space-switcher.ts index 353c332..f5817dc 100644 --- a/shared/components/rstack-space-switcher.ts +++ b/shared/components/rstack-space-switcher.ts @@ -9,7 +9,7 @@ * Passes auth token so the API returns private spaces the user can access. */ -import { isAuthenticated, getAccessToken, getUsername } from "./rstack-identity"; +import { isAuthenticated, getAccessToken } from "./rstack-identity"; import { rspaceNavUrl, getCurrentModule as getModule } from "../url-helpers"; interface SpaceInfo { @@ -264,7 +264,9 @@ export class RStackSpaceSwitcher extends HTMLElement { // Find on page and open auth modal const identity = document.querySelector("rstack-identity") as any; if (identity?.showAuthModal) { - identity.showAuthModal(); + identity.showAuthModal({ + onSuccess: () => this.#autoProvision(), + }); } } else { // Authenticated but no owned space — auto-provision @@ -275,9 +277,10 @@ export class RStackSpaceSwitcher extends HTMLElement { async #autoProvision() { const token = getAccessToken(); - const username = getUsername(); if (!token) return; + const moduleId = this.#getCurrentModule(); + try { const res = await fetch("/api/spaces/auto-provision", { method: "POST", @@ -288,13 +291,10 @@ export class RStackSpaceSwitcher extends HTMLElement { }); const data = await res.json(); if (data.slug) { - window.location.href = `/${data.slug}/canvas`; - } else if (username) { - window.location.href = `/${username}/canvas`; + window.location.href = rspaceNavUrl(data.slug, moduleId); } } catch { - // Fallback: redirect to username path - if (username) window.location.href = `/${username}/canvas`; + // Silently fail — autoResolveSpace in identity may still handle it } }