fix(web): unstick /connect after in-modal sign-in by redirecting to the authorize endpoint (#5133)
This commit is contained in:
1 parent
6e6d1b4941
commit
a7c5ad5db1
5 files changed
+68
-11
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -15,5 +15,7 @@ export function resolveClerkSignInProps(href: string, isElectron: boolean): Cler
|
||||
signUpForceRedirectUrl: 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 };
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
Reference in new issue
Block a user