|
| 1 | +import { Type } from "@/components/ui/type"; |
| 2 | +import { getServerURL } from "@/lib/utils"; |
| 3 | +import { Button, Icon, Stack } from "@speakeasy-api/moonshine"; |
| 4 | +import { useCallback, useEffect, useState } from "react"; |
| 5 | +import { useParams, useNavigate } from "react-router"; |
| 6 | + |
| 7 | +type AuthInfo = { |
| 8 | + appName: string; |
| 9 | + toolsets: { name: string; slug: string }[]; |
| 10 | + token: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type PageState = |
| 14 | + | "loading" |
| 15 | + | "awaiting_login" |
| 16 | + | "completing" |
| 17 | + | "complete" |
| 18 | + | "error"; |
| 19 | + |
| 20 | +export default function SlackLogin() { |
| 21 | + const { token } = useParams<{ token: string }>(); |
| 22 | + const navigate = useNavigate(); |
| 23 | + const [state, setState] = useState<PageState>("loading"); |
| 24 | + const [authInfo, setAuthInfo] = useState<AuthInfo | null>(null); |
| 25 | + const [errorMessage, setErrorMessage] = useState(""); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + if (!token) { |
| 29 | + setState("error"); |
| 30 | + setErrorMessage("No token provided."); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + fetch(`${getServerURL()}/rpc/slack-apps/auth/${token}`, { |
| 35 | + credentials: "include", |
| 36 | + }) |
| 37 | + .then((res) => { |
| 38 | + if (!res.ok) { |
| 39 | + throw new Error("Link expired or invalid"); |
| 40 | + } |
| 41 | + return res.json(); |
| 42 | + }) |
| 43 | + .then((data: AuthInfo) => { |
| 44 | + setAuthInfo(data); |
| 45 | + setState("awaiting_login"); |
| 46 | + }) |
| 47 | + .catch((err) => { |
| 48 | + setState("error"); |
| 49 | + setErrorMessage(err.message || "Failed to load auth info"); |
| 50 | + }); |
| 51 | + }, [token]); |
| 52 | + |
| 53 | + const completeAuth = useCallback(async () => { |
| 54 | + if (!token) return; |
| 55 | + setState("completing"); |
| 56 | + |
| 57 | + try { |
| 58 | + const res = await fetch( |
| 59 | + `${getServerURL()}/rpc/slack-apps/auth/${token}/complete`, |
| 60 | + { |
| 61 | + method: "POST", |
| 62 | + credentials: "include", |
| 63 | + }, |
| 64 | + ); |
| 65 | + |
| 66 | + if (res.status === 401) { |
| 67 | + // Not logged in — redirect to login with redirect back here |
| 68 | + navigate(`/login?redirect=/slack/login/${token}`); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (!res.ok) { |
| 73 | + throw new Error("Failed to complete authentication"); |
| 74 | + } |
| 75 | + |
| 76 | + setState("complete"); |
| 77 | + } catch (err) { |
| 78 | + setState("error"); |
| 79 | + setErrorMessage( |
| 80 | + err instanceof Error ? err.message : "Something went wrong", |
| 81 | + ); |
| 82 | + } |
| 83 | + }, [token, navigate]); |
| 84 | + |
| 85 | + // Auto-attempt completion on mount if user might already be logged in |
| 86 | + useEffect(() => { |
| 87 | + if (state === "awaiting_login") { |
| 88 | + // Try completing — if it fails with 401, we'll show the sign-in button |
| 89 | + completeAuth(); |
| 90 | + } |
| 91 | + }, [state === "awaiting_login"]); // eslint-disable-line react-hooks/exhaustive-deps |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="flex min-h-screen items-center justify-center bg-background p-4"> |
| 95 | + <div className="w-full max-w-md rounded-xl border bg-card p-8 shadow-sm"> |
| 96 | + <Stack gap={6} align="center"> |
| 97 | + <div className="flex h-12 w-12 items-center justify-center rounded-full bg-muted/50"> |
| 98 | + <Icon name="slack" className="h-6 w-6 text-muted-foreground" /> |
| 99 | + </div> |
| 100 | + |
| 101 | + {state === "loading" && ( |
| 102 | + <Stack gap={2} align="center"> |
| 103 | + <Icon |
| 104 | + name="loader-circle" |
| 105 | + className="h-6 w-6 animate-spin text-muted-foreground" |
| 106 | + /> |
| 107 | + <Type muted small> |
| 108 | + Loading... |
| 109 | + </Type> |
| 110 | + </Stack> |
| 111 | + )} |
| 112 | + |
| 113 | + {state === "completing" && ( |
| 114 | + <Stack gap={2} align="center"> |
| 115 | + <Icon |
| 116 | + name="loader-circle" |
| 117 | + className="h-6 w-6 animate-spin text-muted-foreground" |
| 118 | + /> |
| 119 | + <Type muted small> |
| 120 | + Linking your account... |
| 121 | + </Type> |
| 122 | + </Stack> |
| 123 | + )} |
| 124 | + |
| 125 | + {state === "awaiting_login" && authInfo && ( |
| 126 | + <Stack gap={4} align="center" className="w-full"> |
| 127 | + <Stack gap={1} align="center"> |
| 128 | + <Type variant="subheading">Sign in to Gram</Type> |
| 129 | + <Type muted small className="text-center"> |
| 130 | + <strong>{authInfo.appName}</strong> needs to verify your |
| 131 | + identity to process your messages. |
| 132 | + </Type> |
| 133 | + </Stack> |
| 134 | + |
| 135 | + {authInfo.toolsets.length > 0 && ( |
| 136 | + <div className="w-full rounded-lg border bg-muted/20 p-3"> |
| 137 | + <Type muted small className="mb-2 block font-medium"> |
| 138 | + MCP Servers |
| 139 | + </Type> |
| 140 | + <Stack gap={1}> |
| 141 | + {authInfo.toolsets.map((ts) => ( |
| 142 | + <div |
| 143 | + key={ts.slug} |
| 144 | + className="flex items-center gap-2 rounded-md px-2 py-1" |
| 145 | + > |
| 146 | + <Icon |
| 147 | + name="network" |
| 148 | + className="h-3.5 w-3.5 text-muted-foreground" |
| 149 | + /> |
| 150 | + <Type small>{ts.name}</Type> |
| 151 | + </div> |
| 152 | + ))} |
| 153 | + </Stack> |
| 154 | + </div> |
| 155 | + )} |
| 156 | + |
| 157 | + <Button |
| 158 | + className="w-full" |
| 159 | + onClick={() => |
| 160 | + navigate(`/login?redirect=/slack/login/${token}`) |
| 161 | + } |
| 162 | + > |
| 163 | + <Button.Text>Sign in to Gram</Button.Text> |
| 164 | + </Button> |
| 165 | + </Stack> |
| 166 | + )} |
| 167 | + |
| 168 | + {state === "complete" && ( |
| 169 | + <Stack gap={3} align="center"> |
| 170 | + <div className="flex h-10 w-10 items-center justify-center rounded-full bg-primary/10"> |
| 171 | + <Icon name="check" className="h-5 w-5 text-primary" /> |
| 172 | + </div> |
| 173 | + <Stack gap={1} align="center"> |
| 174 | + <Type variant="subheading">You're connected!</Type> |
| 175 | + <Type muted small className="text-center"> |
| 176 | + You can close this page and return to Slack. |
| 177 | + </Type> |
| 178 | + </Stack> |
| 179 | + </Stack> |
| 180 | + )} |
| 181 | + |
| 182 | + {state === "error" && ( |
| 183 | + <Stack gap={3} align="center"> |
| 184 | + <div className="flex h-10 w-10 items-center justify-center rounded-full bg-destructive/10"> |
| 185 | + <Icon name="circle-x" className="h-5 w-5 text-destructive" /> |
| 186 | + </div> |
| 187 | + <Stack gap={1} align="center"> |
| 188 | + <Type variant="subheading">Link expired</Type> |
| 189 | + <Type muted small className="text-center"> |
| 190 | + {errorMessage || |
| 191 | + "This login link is no longer valid. Send another message in Slack to get a new one."} |
| 192 | + </Type> |
| 193 | + </Stack> |
| 194 | + </Stack> |
| 195 | + )} |
| 196 | + </Stack> |
| 197 | + </div> |
| 198 | + </div> |
| 199 | + ); |
| 200 | +} |
0 commit comments