// 1. Request authorization with openid scope
const params = new URLSearchParams({
response_type: 'code',
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
scope: 'openid profile email',
state: state,
nonce: nonce // For ID token validation
});
window.location = `${AUTHORITY_URL}/authorize?${params}`;
// 2. Exchange code for tokens
const tokens = await fetch('/token', {
method: 'POST',
body: new URLSearchParams({
grant_type: 'authorization_code',
code: code,
redirect_uri: REDIRECT_URI
})
}).then(r => r.json());
// 3. Validate and use ID token
const idToken = parseIdToken(tokens.id_token);
console.log('User:', idToken.name);
// 4. Optionally fetch more claims
const userinfo = await fetch('/userinfo', {
headers: { 'Authorization': `Bearer ${tokens.access_token}` }
}).then(r => r.json());