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

pair.tsx

47 lines1.2 KB

1import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router";
2​
3import { PairingPendingSurface, PairingRouteSurface } from "../components/auth/PairingRouteSurface";
4import {
5 ensurePrimaryEnvironmentReady,
6 resolveInitialServerAuthGateState,
7} from "../environments/primary";
8​
9export const Route = createFileRoute("/pair")({
10 beforeLoad: async () => {
11 const [, authGateState] = await Promise.all([
12 ensurePrimaryEnvironmentReady(),
13 resolveInitialServerAuthGateState(),
14 ]);
15 if (authGateState.status === "authenticated") {
16 throw redirect({ to: "/", replace: true });
17 }
18 return {
19 authGateState,
20 };
21 },
22 component: PairRouteView,
23 pendingComponent: PairRoutePendingView,
24});
25​
26function PairRouteView() {
27 const { authGateState } = Route.useRouteContext();
28 const navigate = useNavigate();
29​
30 if (!authGateState) {
31 return null;
32 }
33​
34 return (
35 <PairingRouteSurface
36 auth={authGateState.auth}
37 onAuthenticated={() => {
38 void navigate({ to: "/", replace: true });
39 }}
40 {...(authGateState.errorMessage ? { initialErrorMessage: authGateState.errorMessage } : {})}
41 />
42 );
44​
45function PairRoutePendingView() {
46 return <PairingPendingSurface />;