Skip to content

Commit 7e4f1ed

Browse files
feat(core): return response from loginWith (#541)
closes #144. closes #411. closes #249.
1 parent 344920c commit 7e4f1ed

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

lib/core/auth.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ export default class Auth {
128128
return Promise.resolve()
129129
}
130130

131-
return this.wrapLogin(this.strategy.login(...arguments)).catch(error => {
132-
this.callOnError(error, { method: 'login' })
133-
return Promise.reject(error)
134-
})
131+
return this.wrapLogin(this.strategy.login(...arguments))
132+
.catch(error => {
133+
this.callOnError(error, { method: 'login' })
134+
return Promise.reject(error)
135+
})
135136
}
136137

137138
fetchUser () {
@@ -259,7 +260,7 @@ export default class Auth {
259260
return this.$storage.getState('busy')
260261
}
261262

262-
request (endpoint, defaults) {
263+
request (endpoint, defaults, withResponse) {
263264
const _endpoint =
264265
typeof defaults === 'object'
265266
? Object.assign({}, defaults, endpoint)
@@ -274,10 +275,15 @@ export default class Auth {
274275
return this.ctx.app.$axios
275276
.request(_endpoint)
276277
.then(response => {
277-
if (_endpoint.propertyName) {
278-
return getProp(response.data, _endpoint.propertyName)
278+
const result = _endpoint.propertyName ? getProp(response.data, _endpoint.propertyName) : response.data
279+
280+
if (withResponse) {
281+
return {
282+
response,
283+
result
284+
}
279285
} else {
280-
return response.data
286+
return result
281287
}
282288
})
283289
.catch(error => {
@@ -310,8 +316,9 @@ export default class Auth {
310316
this.error = null
311317

312318
return Promise.resolve(promise)
313-
.then(() => {
319+
.then(response => {
314320
this.$storage.setState('busy', false)
321+
return response
315322
})
316323
.catch(error => {
317324
this.$storage.setState('busy', false)

lib/schemes/local.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ export default class LocalScheme {
3737
// Ditch any leftover local tokens before attempting to log in
3838
await this.$auth.reset()
3939

40-
const result = await this.$auth.request(
40+
const { response, result } = await this.$auth.request(
4141
endpoint,
42-
this.options.endpoints.login
42+
this.options.endpoints.login,
43+
true
4344
)
4445

4546
if (this.options.tokenRequired) {
@@ -54,6 +55,8 @@ export default class LocalScheme {
5455
if (this.options.autoFetchUser) {
5556
await this.fetchUser()
5657
}
58+
59+
return response
5760
}
5861

5962
async setUserToken (tokenValue) {

test/module.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ describe('auth', () => {
4040
await page.goto(url('/'))
4141
await page.waitForFunction('!!window.$nuxt')
4242

43-
const { token, user, axiosBearer } = await page.evaluate(async () => {
44-
await window.$nuxt.$auth.loginWith('local', {
43+
const { token, user, axiosBearer, response } = await page.evaluate(async () => {
44+
const response = await window.$nuxt.$auth.loginWith('local', {
4545
data: { username: 'test_username', password: '123' }
4646
})
4747

4848
return {
4949
axiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization,
5050
token: window.$nuxt.$auth.getToken('local'),
51-
user: window.$nuxt.$auth.user
51+
user: window.$nuxt.$auth.user,
52+
response
5253
}
5354
})
5455

@@ -58,6 +59,7 @@ describe('auth', () => {
5859
expect(token).toBeDefined()
5960
expect(user).toBeDefined()
6061
expect(user.username).toBe('test_username')
62+
expect(response).toBeDefined()
6163

6264
await page.close()
6365
})

0 commit comments

Comments
 (0)