From 8211b98f90ba26cc0b885b41eed85ab020681a7f Mon Sep 17 00:00:00 2001 From: David Luecke Date: Sat, 20 Jul 2019 10:53:10 -0700 Subject: [PATCH] fix: Do not error in authentication client on logout (#1473) --- packages/authentication-client/src/core.ts | 24 ++++++++++++------- .../authentication-client/test/index.test.ts | 10 ++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/authentication-client/src/core.ts b/packages/authentication-client/src/core.ts index 4c0c898c20..d5385878e5 100644 --- a/packages/authentication-client/src/core.ts +++ b/packages/authentication-client/src/core.ts @@ -166,14 +166,20 @@ export class AuthenticationClient { logout () { return Promise.resolve(this.app.get('authentication')) - .then(() => this.service.remove(null)) - .then((authResult: AuthenticationResult) => this.removeAccessToken() - .then(() => this.reset()) - .then(() => { - this.app.emit('logout', authResult); - - return authResult; - }) - ); + .then(auth => { + if (!auth) { + return null; + } + + return this.service.remove(null) + .then((authResult: AuthenticationResult) => this.removeAccessToken() + .then(() => this.reset()) + .then(() => { + this.app.emit('logout', authResult); + + return authResult; + }) + ); + }); } } diff --git a/packages/authentication-client/test/index.test.ts b/packages/authentication-client/test/index.test.ts index e0f84d3d00..3142b16f51 100644 --- a/packages/authentication-client/test/index.test.ts +++ b/packages/authentication-client/test/index.test.ts @@ -24,6 +24,10 @@ describe('@feathersjs/authentication-client', () => { }, remove (id) { + if (!app.get('authentication')) { + throw new Error('Not logged in'); + } + return Promise.resolve({ id }); } }); @@ -131,6 +135,12 @@ describe('@feathersjs/authentication-client', () => { }); }); + it('logout when not logged in without error', async () => { + const result = await app.logout(); + + assert.strictEqual(result, null); + }); + describe('reauthenticate', () => { it('fails when no token in storage', () => { return app.authentication.reAuthenticate().then(() => {