Skip to content

Commit e12106c

Browse files
authored
Merge pull request #303 from internxt/feat/add-logout-endpoint
[_]: feat/implement-logout
2 parents a9ebb57 + fff6759 commit e12106c

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/commands/logout.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Command } from '@oclif/core';
22
import { ConfigService } from '../services/config.service';
33
import { CLIUtils } from '../utils/cli.utils';
4+
import { AuthService } from '../services/auth.service';
45

56
export default class Logout extends Command {
67
static readonly args = {};
@@ -13,6 +14,7 @@ export default class Logout extends Command {
1314
public run = async () => {
1415
const user = await ConfigService.instance.readUser();
1516
if (user) {
17+
await AuthService.instance.logout();
1618
await ConfigService.instance.clearUser();
1719
const message = 'User logged out successfully.';
1820
CLIUtils.success(this.log.bind(this), message);

src/services/auth.service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,24 @@ export class AuthService {
137137
});
138138
return loginCreds;
139139
};
140+
141+
/**
142+
* Logs the user out of the application by invoking the logout method
143+
* from the authentication client. This will terminate the user's session
144+
* and clear any associated authentication data.
145+
*
146+
* @returns A promise that resolves when the logout process is complete.
147+
*/
148+
public logout = async (): Promise<void> => {
149+
try {
150+
const user = await ConfigService.instance.readUser();
151+
if (!user || !user.newToken) {
152+
return;
153+
}
154+
const authClient = SdkManager.instance.getAuth();
155+
return authClient.logout(user.newToken);
156+
} catch {
157+
//no op
158+
}
159+
};
140160
}

test/commands/logout.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
22
import { ConfigService } from '../../src/services/config.service';
33
import { UserCredentialsFixture } from '../fixtures/login.fixture';
44
import Logout from '../../src/commands/logout';
5+
import { AuthService } from '../../src/services/auth.service';
56

67
describe('Logout Command', () => {
78
beforeEach(() => {
@@ -10,6 +11,7 @@ describe('Logout Command', () => {
1011

1112
it('When user is logged out, then it returns false', async () => {
1213
const readUserSpy = vi.spyOn(ConfigService.instance, 'readUser').mockResolvedValue(undefined);
14+
const networkLogout = vi.spyOn(AuthService.instance, 'logout').mockRejectedValue(new Error());
1315
const clearUserSpy = vi.spyOn(ConfigService.instance, 'clearUser').mockRejectedValue(new Error());
1416

1517
const message = 'No user is currently logged in.';
@@ -19,11 +21,13 @@ describe('Logout Command', () => {
1921

2022
expect(result).to.be.deep.equal(expected);
2123
expect(readUserSpy).toHaveBeenCalledOnce();
24+
expect(networkLogout).not.toHaveBeenCalled();
2225
expect(clearUserSpy).not.toHaveBeenCalled();
2326
});
2427

2528
it('When user is logged in, then the current user logged out', async () => {
2629
const readUserSpy = vi.spyOn(ConfigService.instance, 'readUser').mockResolvedValue(UserCredentialsFixture);
30+
const networkLogout = vi.spyOn(AuthService.instance, 'logout').mockResolvedValue();
2731
const clearUserSpy = vi.spyOn(ConfigService.instance, 'clearUser').mockResolvedValue();
2832

2933
const message = 'User logged out successfully.';
@@ -33,6 +37,7 @@ describe('Logout Command', () => {
3337

3438
expect(result).to.be.deep.equal(expected);
3539
expect(readUserSpy).toHaveBeenCalledOnce();
40+
expect(networkLogout).toHaveBeenCalledOnce();
3641
expect(clearUserSpy).toHaveBeenCalledOnce();
3742
});
3843
});

0 commit comments

Comments
 (0)