Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(web): unstick /connect after in-modal sign-in by redirecting to the authorize endpoint (#5133)
Torben Wettera7c5ad5db167
diff --git a/apps/web/src/cloud/connectCliAuth.test.ts b/apps/web/src/cloud/connectCliAuth.test.tsindex 59b443a49..3d41c4166 100644--- a/apps/web/src/cloud/connectCliAuth.test.ts+++ b/apps/web/src/cloud/connectCliAuth.test.ts@@ -2,6 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vite-plus/test";import {buildConnectCliClerkAuthorizeUrl,+ connectCliSignInRedirectUrl,hasConnectCliAuthConfig,readConnectCliCallbackResult,} from "./connectCliAuth";@@ -69,6 +70,29 @@ describe("connectCliAuth", () => {).toBeNull();});+ it("sends the sign-in redirect to the authorize endpoint, not back to /connect", () => {+ vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);+ vi.stubEnv("VITE_CLERK_CLI_OAUTH_CLIENT_ID", "oauthapp_123");++ const connectUrl = "https://app.t3.codes/connect#state=state-1&challenge=challenge-1";+ const redirectUrl = connectCliSignInRedirectUrl(+ { state: "state-1", challenge: "challenge-1" },+ connectUrl,+ );++ expect(redirectUrl).not.toBe(connectUrl);+ expect(new URL(redirectUrl).pathname).toBe("/oauth/authorize");+ });++ it("falls back to the current URL when the authorize URL cannot be built", () => {+ vi.stubEnv("VITE_CLERK_PUBLISHABLE_KEY", TEST_PUBLISHABLE_KEY);++ const connectUrl = "https://app.t3.codes/connect#state=state-1&challenge=challenge-1";+ expect(+ connectCliSignInRedirectUrl({ state: "state-1", challenge: "challenge-1" }, connectUrl),+ ).toBe(connectUrl);+ });+it("reads the code and state Clerk echoes back to the callback", () => {expect(readConnectCliCallbackResult(diff --git a/apps/web/src/cloud/connectCliAuth.ts b/apps/web/src/cloud/connectCliAuth.tsindex 969215d97..815715da2 100644--- a/apps/web/src/cloud/connectCliAuth.ts+++ b/apps/web/src/cloud/connectCliAuth.ts@@ -60,6 +60,23 @@ export function buildConnectCliClerkAuthorizeUrl(request: ConnectAuthorizeReques});}+/**+ * Where Clerk sends the browser once the sign-in modal on /connect completes.+ * It has to be the authorize endpoint rather than this page: /connect carries+ * the CLI request in its fragment, so navigating back to the same URL is a+ * same-document fragment navigation the browser never reloads — and Clerk+ * treats any post-sign-in navigation as a page unload and skips the state emit+ * that would otherwise re-render the surface, so the session never arrives+ * either. Falls back to the current URL when the authorize URL cannot be+ * built, which only happens on a deployment without the CLI OAuth config.+ */+export function connectCliSignInRedirectUrl(+ request: ConnectAuthorizeRequest,+ currentHref: string,+): string {+ return buildConnectCliClerkAuthorizeUrl(request) ?? currentHref;+}+export function rememberConnectCliAuthState(state: string): void {try {window.sessionStorage.setItem(CONNECT_CLI_AUTH_STATE_STORAGE_KEY, state);diff --git a/apps/web/src/components/clerk/authRedirect.test.ts b/apps/web/src/components/clerk/authRedirect.test.tsindex 140474120..e948d1d9c 100644--- a/apps/web/src/components/clerk/authRedirect.test.ts+++ b/apps/web/src/components/clerk/authRedirect.test.ts@@ -5,7 +5,10 @@ import { resolveClerkSignInProps } from "./authRedirect";describe("resolveClerkSignInProps", () => {it("returns to the current browser URL on the web", () => {const href = "https://app.t3.codes/connect?state=state-1#details";- expect(resolveClerkSignInProps(href, false)).toEqual({ forceRedirectUrl: href });+ expect(resolveClerkSignInProps(href, false)).toEqual({+ forceRedirectUrl: href,+ signUpForceRedirectUrl: href,+ });});it("removes a Clerk virtual pathname and callback params while preserving the desktop route", () => {diff --git a/apps/web/src/components/clerk/authRedirect.ts b/apps/web/src/components/clerk/authRedirect.tsindex 251c5ee36..e0b07241c 100644--- a/apps/web/src/components/clerk/authRedirect.ts+++ b/apps/web/src/components/clerk/authRedirect.ts@@ -15,5 +15,7 @@ export function resolveClerkSignInProps(href: string, isElectron: boolean): ClersignUpForceRedirectUrl: redirectUrl.toString(),};}- return { forceRedirectUrl: href };+ // The sign-in modal can switch to sign-up, which follows its own redirect+ // target; without one Clerk falls back to the URL the modal was opened from.+ return { forceRedirectUrl: href, signUpForceRedirectUrl: href };}diff --git a/apps/web/src/components/cloud/ConnectCliAuthSurface.tsx b/apps/web/src/components/cloud/ConnectCliAuthSurface.tsxindex e47d8ddf7..5d5c280bb 100644--- a/apps/web/src/components/cloud/ConnectCliAuthSurface.tsx+++ b/apps/web/src/components/cloud/ConnectCliAuthSurface.tsx@@ -1,9 +1,10 @@import { useAuth, useClerk, useUser } from "@clerk/react";import { encodeConnectAuthCode, readConnectAuthorizeRequest } from "@t3tools/shared/connectAuth";-import { useEffect, useRef, useState } from "react";+import { useCallback, useEffect, useRef, useState } from "react";import {buildConnectCliClerkAuthorizeUrl,+ connectCliSignInRedirectUrl,readConnectCliAuthState,readConnectCliCallbackResult,rememberConnectCliAuthState,@@ -56,6 +57,21 @@ export function ConnectCliAuthorizeSurface() {const signInOpened = useRef(false);const redirecting = useRef(false);+ const openSignIn = useCallback(() => {+ if (!request) {+ return;+ }+ // Clerk redirects to the authorize endpoint itself once sign-in completes,+ // so the callback's state check has to be armed before handing off.+ rememberConnectCliAuthState(request.state);+ clerk.openSignIn(+ resolveClerkSignInProps(+ connectCliSignInRedirectUrl(request, window.location.href),+ isElectron,+ ),+ );+ }, [clerk, request]);+useEffect(() => {if (!request || !isLoaded || redirecting.current) {return;@@ -63,7 +79,7 @@ export function ConnectCliAuthorizeSurface() {if (!isSignedIn) {if (!signInOpened.current) {signInOpened.current = true;- clerk.openSignIn(resolveClerkSignInProps(window.location.href, isElectron));+ openSignIn();}return;}@@ -74,7 +90,7 @@ export function ConnectCliAuthorizeSurface() {redirecting.current = true;rememberConnectCliAuthState(request.state);window.location.assign(authorizeUrl);- }, [clerk, isLoaded, isSignedIn, request]);+ }, [isLoaded, isSignedIn, openSignIn, request]);if (!request) {return (@@ -101,12 +117,7 @@ export function ConnectCliAuthorizeSurface() {/>{isLoaded && !isSignedIn ? (<div className="mt-6">- <Button- type="button"- onClick={() =>- clerk.openSignIn(resolveClerkSignInProps(window.location.href, isElectron))- }- >+ <Button type="button" onClick={openSignIn}>Sign in</Button></div>