({ setIsAuthenticated }: CallbackProps)
| 9 | } |
| 10 | |
| 11 | const Callback = ({ setIsAuthenticated }: CallbackProps) => { |
| 12 | const [searchParams] = useSearchParams(); |
| 13 | const navigate = useNavigate(); |
| 14 | const [error, setError] = useState<string | null>(null); |
| 15 | |
| 16 | useEffect(() => { |
| 17 | const code = searchParams.get("code"); |
| 18 | |
| 19 | if (!code) { |
| 20 | setError("No code provided by GitHub"); |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | const authenticateWithGitHub = async () => { |
| 25 | try { |
| 26 | const response = await fetch(`/api/code?code=${code}`, { |
| 27 | method: 'GET', |
| 28 | credentials: 'include', // Important to include cookies |
| 29 | }); |
| 30 | |
| 31 | if (!response.ok) { |
| 32 | throw new Error(`Authentication failed: ${response.statusText}`); |
| 33 | } |
| 34 | |
| 35 | // Set authentication state to true |
| 36 | setIsAuthenticated(true); |
| 37 | |
| 38 | // Redirect to dashboard or home page |
| 39 | navigate("/dashboard"); |
| 40 | } catch (err) { |
| 41 | setError(err instanceof Error ? err.message : "Authentication failed"); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | authenticateWithGitHub(); |
| 46 | }, [searchParams, navigate, setIsAuthenticated]); |
| 47 | |
| 48 | if (error) { |
| 49 | return ( |
| 50 | <div className="flex justify-center items-center min-h-screen bg-gradient-to-b from-background to-background/80"> |
| 51 | <Card className="w-[380px] border-destructive/20 shadow-lg"> |
| 52 | <CardHeader className="text-center pb-2"> |
| 53 | <div className="mx-auto bg-destructive/10 p-3 rounded-full mb-3"> |
| 54 | <AlertCircle className="h-6 w-6 text-destructive" /> |
| 55 | </div> |
| 56 | <CardTitle>授权失败</CardTitle> |
| 57 | </CardHeader> |
| 58 | <CardContent className="text-center pb-4"> |
| 59 | <p className="text-muted-foreground">{error}</p> |
| 60 | </CardContent> |
| 61 | <CardFooter className="flex justify-center"> |
| 62 | <Button onClick={() => navigate("/login")}> |
| 63 | 返回登录页面 |
| 64 | </Button> |
| 65 | </CardFooter> |
| 66 | </Card> |
| 67 | </div> |
| 68 | ); |
nothing calls this directly
no test coverage detected