Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/eas-cli/src/user/SessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ export default class SessionManager {
}

public async logoutAsync(): Promise<void> {
const sessionSecret = this.getSessionSecret();
if (sessionSecret) {
const apiV2Client = new ApiV2Client({ accessToken: null, sessionSecret });
try {
await apiV2Client.postAsync('auth/logout', { body: {} });
} catch (e) {
// Best-effort: clear the local session even if the server request fails
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to print the error to the debug log. This way most users don't see it but we also don't suppress the error when debugging.

Log.debug('Failed to invalidate session secret on server:', e);
}
}
this.currentActor = undefined;
await this.setSessionAsync(undefined);
}
Expand Down
32 changes: 32 additions & 0 deletions packages/eas-cli/src/user/__tests__/SessionManager-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,38 @@ describe(SessionManager, () => {
await sessionManager.logoutAsync();
expect(sessionManager['getSessionSecret']()).toBe(null);
});

it('calls the server logout endpoint when session secret exists', async () => {
jest.mocked(fetchSessionSecretAndUserAsync).mockResolvedValue({
sessionSecret: 'SESSION_SECRET',
id: 'USER_ID',
username: 'USERNAME',
});
const apiV2PostSpy = jest.spyOn(ApiV2Client.prototype, 'postAsync');

const sessionManager = new SessionManager(analytics);
await sessionManager['loginAsync']({ username: 'USERNAME', password: 'PASSWORD' });
await sessionManager.logoutAsync();

expect(apiV2PostSpy).toHaveBeenCalledWith('auth/logout', { body: {} });
});

it('clears the local session even if the server logout call fails', async () => {
jest.mocked(fetchSessionSecretAndUserAsync).mockResolvedValue({
sessionSecret: 'SESSION_SECRET',
id: 'USER_ID',
username: 'USERNAME',
});
jest
.spyOn(ApiV2Client.prototype, 'postAsync')
.mockRejectedValueOnce(new Error('Network error'));

const sessionManager = new SessionManager(analytics);
await sessionManager['loginAsync']({ username: 'USERNAME', password: 'PASSWORD' });
await sessionManager.logoutAsync();

expect(sessionManager['getSessionSecret']()).toBe(null);
});
});

describe('showLoginPromptAsync', () => {
Expand Down
Loading