Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/commands/logout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from '@oclif/core';
import { ConfigService } from '../services/config.service';
import { CLIUtils } from '../utils/cli.utils';
import { AuthService } from '../services/auth.service';

export default class Logout extends Command {
static readonly args = {};
Expand All @@ -13,6 +14,7 @@ export default class Logout extends Command {
public run = async () => {
const user = await ConfigService.instance.readUser();
if (user) {
await AuthService.instance.logout();
await ConfigService.instance.clearUser();
const message = 'User logged out successfully.';
CLIUtils.success(this.log.bind(this), message);
Expand Down
20 changes: 20 additions & 0 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,24 @@ export class AuthService {
});
return loginCreds;
};

/**
* Logs the user out of the application by invoking the logout method
* from the authentication client. This will terminate the user's session
* and clear any associated authentication data.
*
* @returns A promise that resolves when the logout process is complete.
*/
public logout = async (): Promise<void> => {
try {
const user = await ConfigService.instance.readUser();
if (!user || !user.newToken) {
return;
}
const authClient = SdkManager.instance.getAuth();
return authClient.logout(user.newToken);
} catch {
//no op
}
};
}
5 changes: 5 additions & 0 deletions test/commands/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { ConfigService } from '../../src/services/config.service';
import { UserCredentialsFixture } from '../fixtures/login.fixture';
import Logout from '../../src/commands/logout';
import { AuthService } from '../../src/services/auth.service';

describe('Logout Command', () => {
beforeEach(() => {
Expand All @@ -10,6 +11,7 @@ describe('Logout Command', () => {

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

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

expect(result).to.be.deep.equal(expected);
expect(readUserSpy).toHaveBeenCalledOnce();
expect(networkLogout).not.toHaveBeenCalled();
expect(clearUserSpy).not.toHaveBeenCalled();
});

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

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

expect(result).to.be.deep.equal(expected);
expect(readUserSpy).toHaveBeenCalledOnce();
expect(networkLogout).toHaveBeenCalledOnce();
expect(clearUserSpy).toHaveBeenCalledOnce();
});
});