| 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]); |