From d3eddd778ffb69131f59bf828ea40c831882f9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Wed, 26 Feb 2020 22:30:56 -0300 Subject: [PATCH 1/8] refactor(core): improve request and wrapLogin to return response --- lib/core/auth.js | 26 +++++++++++++++++--------- lib/schemes/local.js | 9 ++++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/core/auth.js b/lib/core/auth.js index b8ec7035d..9a33b6d47 100644 --- a/lib/core/auth.js +++ b/lib/core/auth.js @@ -128,10 +128,12 @@ export default class Auth { return Promise.resolve() } - return this.wrapLogin(this.strategy.login(...arguments)).catch(error => { - this.callOnError(error, { method: 'login' }) - return Promise.reject(error) - }) + return this.wrapLogin(this.strategy.login(...arguments)) + .then(response => response) + .catch(error => { + this.callOnError(error, { method: 'login' }) + return Promise.reject(error) + }) } fetchUser () { @@ -259,7 +261,7 @@ export default class Auth { return this.$storage.getState('busy') } - request (endpoint, defaults) { + request (endpoint, defaults, withResponse) { const _endpoint = typeof defaults === 'object' ? Object.assign({}, defaults, endpoint) @@ -274,10 +276,15 @@ export default class Auth { return this.ctx.app.$axios .request(_endpoint) .then(response => { - if (_endpoint.propertyName) { - return getProp(response.data, _endpoint.propertyName) + const result = _endpoint.propertyName ? getProp(response.data, _endpoint.propertyName) : response.data + + if (withResponse) { + return { + response, + result + } } else { - return response.data + return result } }) .catch(error => { @@ -310,8 +317,9 @@ export default class Auth { this.error = null return Promise.resolve(promise) - .then(() => { + .then(response => { this.$storage.setState('busy', false) + return response }) .catch(error => { this.$storage.setState('busy', false) diff --git a/lib/schemes/local.js b/lib/schemes/local.js index 38fbc515a..34998a332 100644 --- a/lib/schemes/local.js +++ b/lib/schemes/local.js @@ -37,9 +37,10 @@ export default class LocalScheme { // Ditch any leftover local tokens before attempting to log in await this._logoutLocally() - const result = await this.$auth.request( + const { response, result } = await this.$auth.request( endpoint, - this.options.endpoints.login + this.options.endpoints.login, + true ) if (this.options.tokenRequired) { @@ -51,7 +52,9 @@ export default class LocalScheme { this._setToken(token) } - return this.fetchUser() + this.fetchUser() + + return response } async setUserToken (tokenValue) { From 18a5fd5d2cd7264b226cd03ac16a97ced8a5981d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Wed, 26 Feb 2020 22:55:58 -0300 Subject: [PATCH 2/8] feat(local scheme): add autoFetchUser option --- lib/schemes/local.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/schemes/local.js b/lib/schemes/local.js index 34998a332..25eccceab 100644 --- a/lib/schemes/local.js +++ b/lib/schemes/local.js @@ -52,7 +52,9 @@ export default class LocalScheme { this._setToken(token) } - this.fetchUser() + if (this.options.autoFetchUser) { + this.fetchUser() + } return response } @@ -119,5 +121,6 @@ const DEFAULTS = { tokenRequired: true, tokenType: 'Bearer', globalToken: true, - tokenName: 'Authorization' + tokenName: 'Authorization', + autoFetchUser: true } From 92d2e0ff869513402ed74618b8f3bd9e3896c557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Wed, 26 Feb 2020 22:56:35 -0300 Subject: [PATCH 3/8] docs(local scheme): add autoFetchUser option --- docs/schemes/local.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/schemes/local.md b/docs/schemes/local.md index 9a731a6b8..1775c7b7c 100644 --- a/docs/schemes/local.md +++ b/docs/schemes/local.md @@ -35,6 +35,7 @@ auth: { }, // tokenRequired: true, // tokenType: 'bearer' + // autoFetchUser: true } } } @@ -83,3 +84,9 @@ This option can be used to disable all token handling. Useful for Cookie only fl - Default: `Bearer` Authorization header type to be used in axios requests. + + ### `autoFetchUser` + + - Default: `true` + + This option can be used to disable user fetch after login. It is useful when your login response already have the user. From fd2e5e1bb686a4ae0757e61d6eba79fb3e98223e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Wed, 26 Feb 2020 23:26:20 -0300 Subject: [PATCH 4/8] fix(local scheme): await fetch user --- lib/schemes/local.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/schemes/local.js b/lib/schemes/local.js index 25eccceab..f0684f3d8 100644 --- a/lib/schemes/local.js +++ b/lib/schemes/local.js @@ -53,7 +53,7 @@ export default class LocalScheme { } if (this.options.autoFetchUser) { - this.fetchUser() + await this.fetchUser() } return response From 23040007bae714405712c3aba7221a78c83a0a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 2 Mar 2020 17:20:15 -0300 Subject: [PATCH 5/8] refactor(core): remove unnecessary line --- lib/core/auth.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/core/auth.js b/lib/core/auth.js index 9a33b6d47..631d8625c 100644 --- a/lib/core/auth.js +++ b/lib/core/auth.js @@ -129,7 +129,6 @@ export default class Auth { } return this.wrapLogin(this.strategy.login(...arguments)) - .then(response => response) .catch(error => { this.callOnError(error, { method: 'login' }) return Promise.reject(error) From 7d0bc40360fd2c2de2e1ecf793bbf7f7487d0155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 2 Mar 2020 17:39:00 -0300 Subject: [PATCH 6/8] revert: "docs(local scheme): add autoFetchUser option" This reverts commit 92d2e0ff --- docs/schemes/local.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/docs/schemes/local.md b/docs/schemes/local.md index 1775c7b7c..9a731a6b8 100644 --- a/docs/schemes/local.md +++ b/docs/schemes/local.md @@ -35,7 +35,6 @@ auth: { }, // tokenRequired: true, // tokenType: 'bearer' - // autoFetchUser: true } } } @@ -84,9 +83,3 @@ This option can be used to disable all token handling. Useful for Cookie only fl - Default: `Bearer` Authorization header type to be used in axios requests. - - ### `autoFetchUser` - - - Default: `true` - - This option can be used to disable user fetch after login. It is useful when your login response already have the user. From 223a59b46d07d44d23673d4081c1ef1f4a0b7b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 2 Mar 2020 17:39:51 -0300 Subject: [PATCH 7/8] revert: "feat(local scheme): add autoFetchUser option" This reverts commit 18a5fd5d2cd7264b226cd03ac16a97ced8a5981d. # Conflicts: # lib/schemes/local.js --- lib/schemes/local.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/schemes/local.js b/lib/schemes/local.js index f0684f3d8..d63497769 100644 --- a/lib/schemes/local.js +++ b/lib/schemes/local.js @@ -52,9 +52,7 @@ export default class LocalScheme { this._setToken(token) } - if (this.options.autoFetchUser) { - await this.fetchUser() - } + await this.fetchUser() return response } @@ -121,6 +119,5 @@ const DEFAULTS = { tokenRequired: true, tokenType: 'Bearer', globalToken: true, - tokenName: 'Authorization', - autoFetchUser: true + tokenName: 'Authorization' } From 52270d6dfcb718fc73f9164d258208cff8494add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Antunes=20Silva?= Date: Mon, 2 Mar 2020 17:43:16 -0300 Subject: [PATCH 8/8] test(login): expect 'response' to be defined --- test/module.test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/module.test.js b/test/module.test.js index 5f2a1ca5d..8635e00a7 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -40,15 +40,16 @@ describe('auth', () => { await page.goto(url('/')) await page.waitForFunction('!!window.$nuxt') - const { token, user, axiosBearer } = await page.evaluate(async () => { - await window.$nuxt.$auth.loginWith('local', { + const { token, user, axiosBearer, response } = await page.evaluate(async () => { + const response = await window.$nuxt.$auth.loginWith('local', { data: { username: 'test_username', password: '123' } }) return { axiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization, token: window.$nuxt.$auth.getToken('local'), - user: window.$nuxt.$auth.user + user: window.$nuxt.$auth.user, + response } }) @@ -58,6 +59,7 @@ describe('auth', () => { expect(token).toBeDefined() expect(user).toBeDefined() expect(user.username).toBe('test_username') + expect(response).toBeDefined() await page.close() })