Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): return response from loginWith #541

Merged
merged 9 commits into from
Mar 3, 2020
25 changes: 16 additions & 9 deletions lib/core/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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)
Expand All @@ -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 => {
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions lib/schemes/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -54,6 +55,8 @@ export default class LocalScheme {
if (this.options.autoFetchUser) {
await this.fetchUser()
}

return response
}

async setUserToken (tokenValue) {
Expand Down
8 changes: 5 additions & 3 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})

Expand All @@ -58,6 +59,7 @@ describe('auth', () => {
expect(token).toBeDefined()
expect(user).toBeDefined()
expect(user.username).toBe('test_username')
expect(response).toBeDefined()

await page.close()
})
Expand Down