Skip to content

Commit 373c42d

Browse files
authored
fix: Skip ChatGPT login when already authenticated (#247)
Closes #243
1 parent b719332 commit 373c42d

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/CodexAcpClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ export class CodexAcpClient {
8888
return await this.authenticateWithApiKey(apiKey);
8989
}
9090
case "chat-gpt": {
91+
const accountResponse = await this.codexClient.accountRead({refreshToken: true});
92+
if (accountResponse.account?.type === "chatgpt") {
93+
this.gatewayConfig = null;
94+
return true;
95+
}
9196
const loginCompletedPromise = this.awaitNextLoginCompleted();
9297
const loginResponse = await this.codexClient.accountLogin({type: "chatgpt"});
9398
if (loginResponse.type == "chatgpt") {

src/__tests__/CodexACPAgent/CodexAcpClient.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ describe('ACP server test', { timeout: 40_000 }, () => {
186186
.rejects.toThrow(`${CODEX_API_KEY_ENV_VAR} or ${OPENAI_API_KEY_ENV_VAR} is not set`);
187187
});
188188

189+
it('should not start ChatGPT login when already authenticated', async () => {
190+
const chatGptFixture = createCodexMockTestFixture();
191+
const codexAppServerClient = chatGptFixture.getCodexAppServerClient();
192+
const accountReadSpy = vi.spyOn(codexAppServerClient, "accountRead").mockResolvedValue({
193+
account: { type: "chatgpt", email: "test@example.com", planType: "pro" },
194+
requiresOpenaiAuth: false,
195+
});
196+
const accountLoginSpy = vi.spyOn(codexAppServerClient, "accountLogin");
197+
198+
await expect(chatGptFixture.getCodexAcpAgent().authenticate({methodId: "chat-gpt"}))
199+
.resolves.toEqual({});
200+
201+
expect(accountReadSpy).toHaveBeenCalledWith({refreshToken: true});
202+
expect(accountLoginSpy).not.toHaveBeenCalled();
203+
});
204+
189205
it('should authenticate with a gateway', async () => {
190206
const gatewayFixture = createTestFixture();
191207
const codexAcpAgent = gatewayFixture.getCodexAcpAgent();

0 commit comments

Comments
 (0)