Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ConnectCliAuthSurface.tsx

115 lines3.3 KB

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