Open History to see the last commit
1import { useAuth, useClerk } from "@clerk/react";
2import { readConnectAuthorizeRequest } from "@t3tools/shared/connectAuth";
3import { useCallback, useEffect, useRef, useState } from "react";
4
5import {
6 buildConnectCliClerkAuthorizeUrl,
7 connectCliSignInRedirectUrl,
8} from "../../cloud/connectCliAuth";
9import { isElectron } from "../../env";
10import { AuthSurfaceShell } from "../auth/AuthSurfaceShell";
11import { resolveClerkSignInProps } from "../clerk/authRedirect";
12import { Button } from "../ui/button";
13
14function ConnectCliAuthMessage({
15 eyebrow,
16 title,
17 description,
18}: {
19 readonly eyebrow?: string;
20 readonly title: string;
21 readonly description: string;
22}) {
23 return (
24 <>
25 {eyebrow ? (
26 <p className="text-3xs font-semibold tracking-widest text-primary uppercase">{eyebrow}</p>
27 ) : null}
28 <h1 className="mt-2 text-2xl font-semibold tracking-tight sm:text-3xl">{title}</h1>
29 <p className="mt-2 text-sm leading-relaxed text-muted-foreground">{description}</p>
30 </>
31 );
32}
33
34const invalidLinkMessage = {
35 eyebrow: "Authorization request",
36 title: "This connect link is incomplete",
37 description:
38 "The link is missing its authorization request. Re-run `t3 connect` in your terminal and open the freshly printed URL.",
39} as const;
40
41/**
42 * /connect: the URL the CLI prints for the loopback flow. Waits for a Clerk
43 * session, then forwards the CLI's PKCE request to Clerk's authorize endpoint
44 * with the loopback redirect URI so the code returns straight to the waiting
45 * CLI. Headless hosts use Clerk's device authorization page instead.
46 */
47export function ConnectCliAuthorizeSurface() {
48 const [request] = useState(() => readConnectAuthorizeRequest(new URL(window.location.href)));
49 const clerk = useClerk();
50 const { isLoaded, isSignedIn } = useAuth();
51 const signInOpened = useRef(false);
52 const redirecting = useRef(false);
53
54 const openSignIn = useCallback(() => {
55 if (!request) {
56 return;
57 }
58 clerk.openSignIn(
59 resolveClerkSignInProps(
60 connectCliSignInRedirectUrl(request, window.location.href),
61 isElectron,
62 ),
63 );
64 }, [clerk, request]);
65
66 useEffect(() => {
67 if (!request || !isLoaded || redirecting.current) {
68 return;
69 }
70 if (!isSignedIn) {
71 if (!signInOpened.current) {
72 signInOpened.current = true;
73 openSignIn();
74 }
75 return;
76 }
77 const authorizeUrl = buildConnectCliClerkAuthorizeUrl(request);
78 if (!authorizeUrl) {
79 return;
80 }
81 redirecting.current = true;
82 window.location.assign(authorizeUrl);
83 }, [isLoaded, isSignedIn, openSignIn, request]);
84
85 if (!request) {
86 return (
87 <AuthSurfaceShell>
88 <ConnectCliAuthMessage {...invalidLinkMessage} />
89 </AuthSurfaceShell>
90 );
91 }
92
93 return (
94 <AuthSurfaceShell>
95 <ConnectCliAuthMessage
96 eyebrow="Browser authorization"
97 title="Connecting your terminal"
98 description={
99 isSignedIn
100 ? "Redirecting to authorize T3 Connect for your CLI…"
101 : "Sign in to continue authorizing T3 Connect for your CLI."
102 }
103 />
104 {isLoaded && !isSignedIn ? (
105 <div className="mt-6">
106 <Button type="button" onClick={openSignIn}>
107 Sign in
108 </Button>
109 </div>
110 ) : null}
111 </AuthSurfaceShell>
112 );
113}