From b5f87f45e561c8599a3f0a79e8aa2f0f262a0fb4 Mon Sep 17 00:00:00 2001 From: mehfuz Date: Sat, 14 Nov 2026 00:38:51 -0800 Subject: [PATCH] Add /gpushed/cli-login to approve a gpushed CLI sign-in gpushed login opens this page with the code shown in the terminal. It sends you through /login first if you're signed out, shows the code, and on Approve calls the gpushed API's /auth/cli/approve with your session; the CLI, polling, then gets its token. --- src/app/gpushed/cli-login/page.tsx | 126 +++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/app/gpushed/cli-login/page.tsx diff --git a/src/app/gpushed/cli-login/page.tsx b/src/app/gpushed/cli-login/page.tsx new file mode 100644 index 0000000..25c9691 --- /dev/null +++ b/src/app/gpushed/cli-login/page.tsx @@ -0,0 +1,126 @@ +'use client'; + +/** + * Approve a gpushed CLI sign-in. `gpushed login` opens + * /gpushed/cli-login?code=XXXX-XXXX; once you approve here, the CLI (which + * polls the gpushed API) gets its token. The code is shown in your terminal + * too: approving only when they match stops someone from getting you to sign + * in their terminal with a link of theirs. + */ + +import { Suspense, useEffect, useState } from 'react'; +import { useSearchParams } from 'next/navigation'; +import { Box } from '@mui/material'; +import CheckCircleIcon from '@mui/icons-material/CheckCircle'; +import AuthHeader from '@/app/components/auth-header'; +import LoadingSpinner from '@/app/components/loading-spinner'; +import { GPUSHED_ENDPOINT } from '@/app/constants'; +import { userService } from '@/app/services/user.service'; + +type State = 'loading' | 'ready' | 'approving' | 'approved' | 'error'; + +function CliLoginContent() { + const searchParams = useSearchParams(); + const code = (searchParams.get('code') ?? '').toUpperCase(); + const [state, setState] = useState('loading'); + const [email, setEmail] = useState(''); + const [error, setError] = useState(''); + + // Signed out: sign in first, then come back here with the same code. + useEffect(() => { + const back = `/gpushed/cli-login?code=${encodeURIComponent(code)}`; + userService + .get() + .then((u: any) => { + if (!u) { + window.location.href = `/login?redirect=${encodeURIComponent(back)}`; + return; + } + setEmail(u.email ?? ''); + setState('ready'); + }) + .catch(() => { + window.location.href = `/login?redirect=${encodeURIComponent(back)}`; + }); + }, [code]); + + const approve = async () => { + setState('approving'); + try { + const r: Response | undefined = await userService.makeAuthenticatedRequest(`${GPUSHED_ENDPOINT}/auth/cli/approve`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ user_code: code }), + }); + if (!r) return; // session expired: makeAuthenticatedRequest sends you to /login + if (r.ok) { + setState('approved'); + return; + } + const body = await r.json().catch(() => ({})); + setError(body.error ?? 'Could not approve this sign-in. Run gpushed login again.'); + setState('error'); + } catch { + setError('Could not reach gpushed. Try again in a moment.'); + setState('error'); + } + }; + + if (state === 'loading') { + return ; + } + + const button = + 'px-8 py-4 rounded-lg font-medium transition-all text-lg w-full text-center bg-pink-500 text-white hover:bg-pink-600 disabled:opacity-60'; + + return ( + + + +
+ {state === 'approved' ? ( +
+ +
+

gpushed is signed in

+

Head back to your terminal; you can close this tab.

+
+
+ ) : !code ? ( +
+

No sign-in code

+

+ Run gpushed login in your terminal; it opens this page with its code. +

+
+ ) : ( +
+
+

Sign in to gpushed?

+

+ Approve only if your terminal shows this code{email && <> and you meant to sign in as {email}}. +

+
+
{code}
+ {state === 'error' &&

{error}

} + +

+ Didn't run gpushed login? Close this tab; nothing is signed in. +

+
+ )} +
+
+
+ ); +} + +export default function GpushedCliLogin() { + return ( + }> + + + ); +}