|
| 1 | +import { isClerkAPIResponseError } from "@clerk/clerk-js"; |
| 2 | +import { useOrganization, useSignIn, useSignUp } from "@clerk/clerk-react"; |
| 3 | +import { createFileRoute, Link, useNavigate } from "@tanstack/react-router"; |
| 4 | +import { useEffect, useState } from "react"; |
| 5 | +import { useIsMounted } from "usehooks-ts"; |
| 6 | +import * as OrgSignUpForm from "@/app/forms/org-sign-up-form"; |
| 7 | +import { Logo } from "@/app/logo"; |
| 8 | +import { |
| 9 | + Button, |
| 10 | + Card, |
| 11 | + CardContent, |
| 12 | + CardDescription, |
| 13 | + CardFooter, |
| 14 | + CardHeader, |
| 15 | + CardTitle, |
| 16 | + toast, |
| 17 | +} from "@/components"; |
| 18 | + |
| 19 | +export const Route = createFileRoute("/onboarding/accept-invitation")({ |
| 20 | + component: RouteComponent, |
| 21 | +}); |
| 22 | + |
| 23 | +function RouteComponent() { |
| 24 | + const search = Route.useSearch(); |
| 25 | + |
| 26 | + if (search.__clerk_status === "sign_up") { |
| 27 | + // display sign up flow |
| 28 | + return ( |
| 29 | + <div className="flex min-h-screen flex-col items-center justify-center bg-background py-4"> |
| 30 | + <div className="flex flex-col items-center gap-6"> |
| 31 | + <Logo className="h-10 mb-4" /> |
| 32 | + <OrgSignUpFlow ticket={search.__clerk_ticket} /> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + if (search.__clerk_status === "sign_in") { |
| 39 | + // complete sign in flow |
| 40 | + return ( |
| 41 | + <div className="flex min-h-screen flex-col items-center justify-center bg-background py-4"> |
| 42 | + <div className="flex flex-col items-center gap-6"> |
| 43 | + <Logo className="h-10 mb-4" /> |
| 44 | + <OrgSignInFlow ticket={search.__clerk_ticket} /> |
| 45 | + </div> |
| 46 | + </div> |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + if (search.__clerk_status === "complete") { |
| 51 | + // if we get here, the user is already signed in |
| 52 | + return ( |
| 53 | + <div className="flex min-h-screen flex-col items-center justify-center bg-background py-4"> |
| 54 | + <div className="flex flex-col items-center gap-6"> |
| 55 | + <Logo className="h-10 mb-4" /> |
| 56 | + <Card className="w-full sm:w-96"> |
| 57 | + <CardHeader> |
| 58 | + <CardTitle>Invitation Accepted</CardTitle> |
| 59 | + <CardDescription> |
| 60 | + You have successfully accepted the invitation. |
| 61 | + You can now proceed to the dashboard. |
| 62 | + </CardDescription> |
| 63 | + </CardHeader> |
| 64 | + <CardFooter> |
| 65 | + <Button asChild> |
| 66 | + <Link to="/">Go Home</Link> |
| 67 | + </Button> |
| 68 | + </CardFooter> |
| 69 | + </Card> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + return ( |
| 76 | + <div className="flex min-h-screen flex-col items-center justify-center bg-background py-4"> |
| 77 | + <div className="flex flex-col items-center gap-6"> |
| 78 | + <Logo className="h-10 mb-4" /> |
| 79 | + <Card className="w-full sm:w-96"> |
| 80 | + <CardHeader> |
| 81 | + <CardTitle>Invalid Invitation</CardTitle> |
| 82 | + <CardDescription> |
| 83 | + The invitation link is invalid. Please check the |
| 84 | + link or contact support. |
| 85 | + </CardDescription> |
| 86 | + </CardHeader> |
| 87 | + </Card> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +function OrgSignUpFlow({ ticket }: { ticket: string }) { |
| 94 | + const { signUp, setActive: setActiveSignUp } = useSignUp(); |
| 95 | + const navigate = useNavigate(); |
| 96 | + |
| 97 | + return ( |
| 98 | + <OrgSignUpForm.Form |
| 99 | + defaultValues={{ password: "" }} |
| 100 | + onSubmit={async ({ password }, form) => { |
| 101 | + try { |
| 102 | + const signUpAttempt = await signUp?.create({ |
| 103 | + strategy: "ticket", |
| 104 | + ticket, |
| 105 | + password, |
| 106 | + }); |
| 107 | + |
| 108 | + if (signUpAttempt?.status === "complete") { |
| 109 | + await setActiveSignUp?.({ |
| 110 | + session: signUpAttempt.createdSessionId, |
| 111 | + }); |
| 112 | + await navigate({ to: "/" }); |
| 113 | + } else { |
| 114 | + console.error( |
| 115 | + "Sign up attempt not complete", |
| 116 | + signUpAttempt, |
| 117 | + ); |
| 118 | + toast.error( |
| 119 | + "An error occurred during sign up. Please try again.", |
| 120 | + ); |
| 121 | + } |
| 122 | + } catch (e) { |
| 123 | + if (isClerkAPIResponseError(e)) { |
| 124 | + for (const error of e.errors) { |
| 125 | + form.setError( |
| 126 | + (error.meta?.paramName || "root") as "root", |
| 127 | + { |
| 128 | + message: error.longMessage, |
| 129 | + }, |
| 130 | + ); |
| 131 | + } |
| 132 | + } else { |
| 133 | + toast.error( |
| 134 | + "An unknown error occurred. Please try again.", |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + }} |
| 139 | + > |
| 140 | + <Card className="w-full sm:w-96"> |
| 141 | + <CardHeader> |
| 142 | + <CardTitle>Welcome!</CardTitle> |
| 143 | + <CardDescription> |
| 144 | + Please set a password for your new account. |
| 145 | + </CardDescription> |
| 146 | + </CardHeader> |
| 147 | + <CardContent> |
| 148 | + <div> |
| 149 | + <OrgSignUpForm.Password className="mb-4" /> |
| 150 | + </div> |
| 151 | + </CardContent> |
| 152 | + <CardFooter> |
| 153 | + <OrgSignUpForm.Submit className="w-full"> |
| 154 | + Continue |
| 155 | + </OrgSignUpForm.Submit> |
| 156 | + </CardFooter> |
| 157 | + </Card> |
| 158 | + </OrgSignUpForm.Form> |
| 159 | + ); |
| 160 | +} |
| 161 | + |
| 162 | +function OrgSignInFlow({ ticket }: { ticket: string }) { |
| 163 | + const { organization } = useOrganization(); |
| 164 | + const { signIn, setActive: setActiveSignIn } = useSignIn(); |
| 165 | + const isMounted = useIsMounted(); |
| 166 | + |
| 167 | + const [error, setError] = useState<string | null>(null); |
| 168 | + |
| 169 | + useEffect(() => { |
| 170 | + async function signInWithTicket() { |
| 171 | + const signInAttempt = await signIn?.create({ |
| 172 | + strategy: "ticket", |
| 173 | + ticket, |
| 174 | + }); |
| 175 | + |
| 176 | + // If the sign-in was successful, set the session to active |
| 177 | + if (signInAttempt?.status === "complete") { |
| 178 | + await setActiveSignIn?.({ |
| 179 | + session: signInAttempt?.createdSessionId, |
| 180 | + }); |
| 181 | + } else { |
| 182 | + // If the sign-in attempt is not complete, check why. |
| 183 | + // User may need to complete further steps. |
| 184 | + console.error(JSON.stringify(signInAttempt, null, 2)); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + signInWithTicket().catch((e) => { |
| 189 | + if (isClerkAPIResponseError(e)) { |
| 190 | + setError(e.message); |
| 191 | + } else { |
| 192 | + setError("An unknown error occurred. Please try again."); |
| 193 | + } |
| 194 | + }); |
| 195 | + }, [isMounted]); |
| 196 | + |
| 197 | + return ( |
| 198 | + <Card className="w-full sm:w-96"> |
| 199 | + <CardHeader> |
| 200 | + <CardTitle>Welcome back!</CardTitle> |
| 201 | + <CardDescription> |
| 202 | + You are signing in to {organization?.name || "your account"} |
| 203 | + . |
| 204 | + </CardDescription> |
| 205 | + </CardHeader> |
| 206 | + {error && ( |
| 207 | + <CardContent> |
| 208 | + <div className="text-destructive">{error}</div> |
| 209 | + </CardContent> |
| 210 | + )} |
| 211 | + </Card> |
| 212 | + ); |
| 213 | +} |
0 commit comments