export default function Login() {
function handleLogin() {
window.location.href = 'https://oauth.provider.com/auth?client_id=abc&redirect_uri=https://myapp.com/callback';
}
return <button onClick={handleLogin}>Login with OAuth</button>;
}Setting window.location.href causes the browser to navigate to the new URL, which in OAuth is the provider's login page. The current app pauses until the OAuth flow completes and redirects back.
In Astro server routes, the url object has a searchParams property to get query parameters. Using url.searchParams.get('code') correctly extracts the OAuth code.
async function exchangeToken(code) { const response = await fetch('https://oauth.provider.com/token', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ code, client_id: 'abc', client_secret: 'xyz' }) }); return response.json(); }
OAuth token endpoints typically require the POST body to be URL encoded with application/x-www-form-urlencoded content type. Sending JSON causes 400 errors.
The state parameter is a random string sent with the OAuth request and checked on return to prevent cross-site request forgery (CSRF). If it doesn't match, the app should reject the response to keep users safe.
Storing tokens in HttpOnly, Secure cookies prevents JavaScript from accessing them, reducing risk of theft via XSS attacks. Other methods expose tokens to client scripts and are less secure.
