import { sni } from 'solar-js-sdk' import { Container, Box, Typography, Alert, Collapse, Button, CircularProgress, Card, CardContent } from '@mui/material' import { useRouter } from 'next/router' import { useEffect, useState } from 'react' import { checkAuthenticatedClient, redirectToLogin, SnAuthTicket } from 'solar-js-sdk' import ErrorIcon from '@mui/icons-material/Error' import CloseIcon from '@mui/icons-material/Close' import CheckIcon from '@mui/icons-material/Check' export default function AccountAuthorize() { useEffect(() => { if (!checkAuthenticatedClient()) redirectToLogin() }, []) const router = useRouter() const [thirdClient, setThirdClient] = useState(null) const [error, setError] = useState(null) const [reverting, setReverting] = useState(false) const [busy, setBusy] = useState(false) function doCallback(ticket: SnAuthTicket) { const url = `${router.query['redirect_uri']}?code=${ticket.grantToken}&state=${router.query['state']}` window.open(url, '_self') } async function fetch() { try { setReverting(true) const resp = await sni.get<{ ticket: SnAuthTicket; client: any }>( '/cgi/id/auth/o/authorize' + window.location.search, {}, ) if (resp.data.ticket) { return doCallback(resp.data.ticket) } setThirdClient(resp.data.client) } catch (err: any) { setError(err.toString()) } finally { setReverting(false) } } useEffect(() => { fetch() }, []) async function confirm() { try { setBusy(true) const resp = await sni.post<{ ticket: SnAuthTicket }>('/cgi/id/auth/o/authorize' + window.location.search) return doCallback(resp.data.ticket) } catch (err: any) { setError(err.toString()) } finally { setBusy(false) } } function decline() { if (window.history.length > 0) { window.history.back() } else { window.close() } } return ( Connect with Solarpass Connect third-party services with Solar Network } severity="error"> {error} {reverting && ( )} {!reverting && ( {thirdClient?.name} {thirdClient?.description} )} ) }