feat: add headless t3 connect setup for SSH hosts (#3749)
Co-authored-by: Claude Fable 5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: codex <[email protected]>
This commit is contained in:
35 files changed
+3018
-145
No files matched your search
@@ -38,11 +38,15 @@ export function SidebarStageBackdrop({ variant }: { variant: SidebarStageBackdro
|
||||
aria-hidden
|
||||
className="sidebar-stage-backdrop pointer-events-none absolute inset-x-0 top-0 z-0 h-20 select-none overflow-hidden"
|
||||
>
|
||||
{variant === "nightly" ? <NightlySkyArt /> : <DevBlueprintArt />}
|
||||
<StageBackdropArt variant={variant} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function StageBackdropArt({ variant }: { variant: SidebarStageBackdropVariant }) {
|
||||
return variant === "nightly" ? <NightlySkyArt /> : <DevBlueprintArt />;
|
||||
}
|
||||
|
||||
const NIGHTLY_STARS: ReadonlyArray<{
|
||||
cx: number;
|
||||
cy: number;
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
import { APP_DISPLAY_NAME, APP_STAGE_LABEL } from "../../branding";
|
||||
import { resolveSidebarStageBackdropVariant, StageBackdropArt } from "../SidebarStageBackdrop";
|
||||
|
||||
/**
|
||||
* Full-screen card for standalone auth pages, mirroring the pairing surface's
|
||||
* treatment. Used by the CLI-connect authorize and callback surfaces.
|
||||
*/
|
||||
export function AuthSurfaceShell({ children }: { readonly children: ReactNode }) {
|
||||
const stageVariant = resolveSidebarStageBackdropVariant(APP_STAGE_LABEL);
|
||||
|
||||
return (
|
||||
<div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-background px-4 py-10 text-foreground sm:px-6">
|
||||
<div className="pointer-events-none absolute inset-0 opacity-80">
|
||||
<div className="absolute inset-x-0 top-0 h-72 bg-[radial-gradient(48rem_20rem_at_top,color-mix(in_srgb,var(--color-blue-500)_12%,transparent),transparent)]" />
|
||||
<div className="absolute inset-0 bg-[linear-gradient(145deg,color-mix(in_srgb,var(--background)_94%,var(--color-black))_0%,var(--background)_62%)]" />
|
||||
</div>
|
||||
|
||||
<section className="relative w-full max-w-xl overflow-hidden rounded-2xl border border-border/80 bg-card/94 shadow-2xl shadow-black/20 backdrop-blur-md">
|
||||
<header className="relative h-24 overflow-hidden bg-[linear-gradient(135deg,#1e61de,#17348e)] text-white">
|
||||
{stageVariant ? (
|
||||
<div className="absolute inset-0" aria-hidden>
|
||||
<StageBackdropArt variant={stageVariant} />
|
||||
</div>
|
||||
) : (
|
||||
<div
|
||||
aria-hidden
|
||||
className="absolute inset-0 bg-[radial-gradient(circle_at_75%_25%,rgba(136,204,255,0.5),transparent_38%),linear-gradient(135deg,#2468df,#172f82)]"
|
||||
/>
|
||||
)}
|
||||
<div className="absolute inset-0 bg-[linear-gradient(to_bottom,transparent_20%,rgba(7,18,55,0.46)_100%)]" />
|
||||
<div className="relative h-full p-5 sm:p-6">
|
||||
<p className="text-[10px] font-semibold tracking-[0.2em] text-white/80 uppercase">
|
||||
{APP_DISPLAY_NAME}
|
||||
</p>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div className="p-6 sm:p-8">{children}</div>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
import { useAuth, useClerk, useUser } from "@clerk/react";
|
||||
import { encodeConnectAuthCode, readConnectAuthorizeRequest } from "@t3tools/shared/connectAuth";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
import {
|
||||
buildConnectCliClerkAuthorizeUrl,
|
||||
readConnectCliAuthState,
|
||||
readConnectCliCallbackResult,
|
||||
rememberConnectCliAuthState,
|
||||
} from "../../cloud/connectCliAuth";
|
||||
import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
|
||||
import { AuthSurfaceShell } from "../auth/AuthSurfaceShell";
|
||||
import { Button } from "../ui/button";
|
||||
|
||||
function ConnectCliAuthMessage({
|
||||
eyebrow,
|
||||
title,
|
||||
description,
|
||||
}: {
|
||||
readonly eyebrow?: string;
|
||||
readonly title: string;
|
||||
readonly description: string;
|
||||
}) {
|
||||
return (
|
||||
<>
|
||||
{eyebrow ? (
|
||||
<p className="text-[10px] font-semibold tracking-[0.18em] text-blue-600 uppercase dark:text-blue-400">
|
||||
{eyebrow}
|
||||
</p>
|
||||
) : null}
|
||||
<h1 className="mt-2 text-2xl font-semibold tracking-tight sm:text-3xl">{title}</h1>
|
||||
<p className="mt-2 text-sm leading-relaxed text-muted-foreground">{description}</p>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
const invalidLinkMessage = {
|
||||
eyebrow: "Authorization request",
|
||||
title: "This connect link is incomplete",
|
||||
description:
|
||||
"The link is missing its authorization request. Re-run `t3 connect` in your terminal and open the freshly printed URL.",
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* /connect: the URL a headless CLI prints. Waits for a Clerk session, then
|
||||
* forwards the CLI's PKCE request to Clerk's authorize endpoint.
|
||||
*/
|
||||
export function ConnectCliAuthorizeSurface() {
|
||||
const [request] = useState(() => readConnectAuthorizeRequest(new URL(window.location.href)));
|
||||
const clerk = useClerk();
|
||||
const { isLoaded, isSignedIn } = useAuth();
|
||||
const signInOpened = useRef(false);
|
||||
const redirecting = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!request || !isLoaded || redirecting.current) {
|
||||
return;
|
||||
}
|
||||
if (!isSignedIn) {
|
||||
if (!signInOpened.current) {
|
||||
signInOpened.current = true;
|
||||
clerk.openSignIn({ forceRedirectUrl: window.location.href });
|
||||
}
|
||||
return;
|
||||
}
|
||||
const authorizeUrl = buildConnectCliClerkAuthorizeUrl(request);
|
||||
if (!authorizeUrl) {
|
||||
return;
|
||||
}
|
||||
redirecting.current = true;
|
||||
rememberConnectCliAuthState(request.state);
|
||||
window.location.assign(authorizeUrl);
|
||||
}, [clerk, isLoaded, isSignedIn, request]);
|
||||
|
||||
if (!request) {
|
||||
return (
|
||||
<AuthSurfaceShell>
|
||||
<ConnectCliAuthMessage {...invalidLinkMessage} />
|
||||
</AuthSurfaceShell>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthSurfaceShell>
|
||||
<ConnectCliAuthMessage
|
||||
eyebrow="Step 1 of 2 · Browser authorization"
|
||||
title="Connecting your terminal"
|
||||
description={
|
||||
isSignedIn
|
||||
? "Redirecting to authorize T3 Connect for your CLI…"
|
||||
: "Sign in to continue authorizing T3 Connect for your CLI."
|
||||
}
|
||||
/>
|
||||
{isLoaded && !isSignedIn ? (
|
||||
<div className="mt-6">
|
||||
<Button
|
||||
type="button"
|
||||
onClick={() => clerk.openSignIn({ forceRedirectUrl: window.location.href })}
|
||||
>
|
||||
Sign in
|
||||
</Button>
|
||||
</div>
|
||||
) : null}
|
||||
</AuthSurfaceShell>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* /connect/callback: Clerk's redirect target. Shows the one-time code the
|
||||
* user enters in the waiting terminal.
|
||||
*/
|
||||
export function ConnectCliCallbackSurface() {
|
||||
const [result] = useState(readConnectCliCallbackResult);
|
||||
const [expectedState] = useState(readConnectCliAuthState);
|
||||
const { user } = useUser();
|
||||
const { copyToClipboard, isCopied } = useCopyToClipboard({ target: "authentication code" });
|
||||
|
||||
if (!result) {
|
||||
return (
|
||||
<AuthSurfaceShell>
|
||||
<ConnectCliAuthMessage
|
||||
eyebrow="Step 2 of 2 · Terminal handoff"
|
||||
title="Authorization did not complete"
|
||||
description="No authorization code was returned. Re-run `t3 connect` in your terminal and try again."
|
||||
/>
|
||||
</AuthSurfaceShell>
|
||||
);
|
||||
}
|
||||
|
||||
// Fail closed: the legitimate callback always lands in the same browser
|
||||
// that visited /connect (which recorded the state), so a missing or
|
||||
// mismatched state means this page was reached some other way — the CSRF
|
||||
// shape the state parameter exists to stop. Refuse to display a code.
|
||||
if (expectedState === null || expectedState !== result.state) {
|
||||
return (
|
||||
<AuthSurfaceShell>
|
||||
<ConnectCliAuthMessage
|
||||
eyebrow="Step 2 of 2 · Terminal handoff"
|
||||
title="This code belongs to a different request"
|
||||
description="This authorization response does not match a connect request started in this browser. Re-run `t3 connect` in your terminal and open the freshly printed URL in this browser."
|
||||
/>
|
||||
</AuthSurfaceShell>
|
||||
);
|
||||
}
|
||||
|
||||
const accountLabel = user?.primaryEmailAddress?.emailAddress ?? user?.username ?? null;
|
||||
const authCode = encodeConnectAuthCode(result);
|
||||
|
||||
return (
|
||||
<AuthSurfaceShell>
|
||||
<ConnectCliAuthMessage
|
||||
eyebrow="Step 2 of 2 · Terminal handoff"
|
||||
title="Almost connected"
|
||||
description={
|
||||
accountLabel
|
||||
? `Enter this code in your waiting terminal to connect it as ${accountLabel}.`
|
||||
: "Enter this code in your waiting terminal to finish connecting."
|
||||
}
|
||||
/>
|
||||
|
||||
<div className="mt-6 overflow-hidden rounded-xl border border-border/80 bg-background/65">
|
||||
<div className="flex items-center justify-between border-b border-border/70 px-4 py-2.5">
|
||||
<span className="text-[10px] font-semibold tracking-[0.16em] text-muted-foreground uppercase">
|
||||
One-time authorization code
|
||||
</span>
|
||||
<span className="font-mono text-[10px] text-muted-foreground">expires shortly</span>
|
||||
</div>
|
||||
<code
|
||||
className="block p-4 font-mono text-sm leading-relaxed break-all select-all"
|
||||
data-testid="connect-auth-code"
|
||||
>
|
||||
{authCode}
|
||||
</code>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 flex items-center gap-3">
|
||||
<Button type="button" onClick={() => copyToClipboard(authCode)}>
|
||||
{isCopied ? "Copied!" : "Copy authorization code"}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<p className="mt-6 text-xs leading-relaxed text-muted-foreground">
|
||||
Only enter this code in a terminal session you started yourself. Anyone holding it can link
|
||||
their machine to your T3 Connect account while it is valid.
|
||||
</p>
|
||||
</AuthSurfaceShell>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user