fix: extract email from id_token for Codex OAuth to allow multiple accounts (#97)

- Parse JWT id_token to extract email in mapTokens function
- Allows database to distinguish between different Codex accounts
- Fixes issue where adding 3rd+ Codex account would update existing connection instead of creating new one
This commit is contained in:
Adarsh
2026-02-21 15:30:37 +05:30
committed by GitHub
parent 862d94b859
commit 1bd5d552c1

View File

@@ -44,10 +44,24 @@ export const codex = {
return await response.json();
},
mapTokens: (tokens) => ({
accessToken: tokens.access_token,
refreshToken: tokens.refresh_token,
idToken: tokens.id_token,
expiresIn: tokens.expires_in,
}),
mapTokens: (tokens) => {
// Extract email from id_token JWT to distinguish between accounts
let email = null;
if (tokens.id_token) {
try {
const payload = tokens.id_token.split(".")[1];
const decoded = JSON.parse(Buffer.from(payload, "base64").toString());
email = decoded.email || null;
} catch {
// Ignore JWT parsing errors
}
}
return {
accessToken: tokens.access_token,
refreshToken: tokens.refresh_token,
idToken: tokens.id_token,
expiresIn: tokens.expires_in,
email,
};
},
};