Update OAuth2-Backend-Approach.md
This commit is contained in:
parent
12c65d6cbb
commit
d393509b1c
@ -248,70 +248,49 @@ curl -X GET "https://www.googleapis.com/oauth2/v3/userinfo" \
|
|||||||
### Frontend Code
|
### Frontend Code
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// DashboardPage.jsx
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
function DashboardPage() {
|
function DashboardPage() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [error, setError] = useState(null);
|
|
||||||
const [dashboardData, setDashboardData] = useState(null);
|
const [dashboardData, setDashboardData] = useState(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function initializeDashboard() {
|
async function loadDashboard() {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
//
|
//
|
||||||
// First verify auth
|
// Single request that both verifies auth and gets data
|
||||||
//
|
//
|
||||||
const authResponse = await fetch('https://myapp/api/verify-auth', {
|
const response = await fetch('https://myapp/api/dashboard-data', {
|
||||||
credentials: 'include',
|
credentials: 'include' // Sends the auth cookie
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!authResponse.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Authentication failed');
|
//
|
||||||
|
// If auth is invalid, the backend will return 401
|
||||||
|
//
|
||||||
|
if (response.status === 401) {
|
||||||
|
navigate('/login');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new Error('Failed to load dashboard');
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
const data = await response.json();
|
||||||
// Auth is valid, now fetch dashboard data
|
|
||||||
//
|
|
||||||
const dataResponse = await fetch('https://myapp/api/dashboard-data', {
|
|
||||||
credentials: 'include',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!dataResponse.ok) {
|
|
||||||
throw new Error('Failed to load dashboard data');
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await dataResponse.json();
|
|
||||||
setDashboardData(data);
|
setDashboardData(data);
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
//
|
|
||||||
// Redirect to login on auth failure
|
|
||||||
//
|
|
||||||
setError(err.message);
|
|
||||||
navigate('/login');
|
navigate('/login');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeDashboard();
|
loadDashboard();
|
||||||
}, [navigate]);
|
}, [navigate]);
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return (
|
return <div>Loading...</div>;
|
||||||
<div className="flex items-center justify-center min-h-screen">
|
|
||||||
<div className="text-center">
|
|
||||||
<h2>Loading Dashboard...</h2>
|
|
||||||
<p>Please wait while we verify your session.</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -321,3 +300,4 @@ function DashboardPage() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user