Skip to content
Permalink
Browse files
feat(core): return response from loginWith (#541)
 closes #144. closes #411. closes #249.
  • Loading branch information
JoaoPedroAS51 committed Mar 3, 2020
1 parent 344920c commit 7e4f1edebde7938428a80154c1fefc2fa24ff9ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
@@ -128,10 +128,11 @@ 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))
.catch(error => {
this.callOnError(error, { method: 'login' })
return Promise.reject(error)
})
}

fetchUser () {
@@ -259,7 +260,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 +275,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 +316,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)
@@ -37,9 +37,10 @@ export default class LocalScheme {
// Ditch any leftover local tokens before attempting to log in
await this.$auth.reset()

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) {
@@ -54,6 +55,8 @@ export default class LocalScheme {
if (this.options.autoFetchUser) {
await this.fetchUser()
}

return response
}

async setUserToken (tokenValue) {
@@ -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()
})

0 comments on commit 7e4f1ed

Please sign in to comment.