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
7 changes: 7 additions & 0 deletions docs/schemes/local.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ auth: {
},
// tokenRequired: true,
// tokenType: 'bearer'
// autoFetchUser: true
}
}
}
Expand Down Expand Up @@ -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`
JoaoPedroAS51 marked this conversation as resolved.
Show resolved Hide resolved

- Default: `true`

This option can be used to disable user fetch after login. It is useful when your login response already have the user.
26 changes: 17 additions & 9 deletions lib/core/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
pi0 marked this conversation as resolved.
Show resolved Hide resolved
.catch(error => {
this.callOnError(error, { method: 'login' })
return Promise.reject(error)
})
}

fetchUser () {
Expand Down Expand Up @@ -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)
Expand All @@ -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 => {
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 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._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) {
Expand All @@ -51,7 +52,11 @@ export default class LocalScheme {
this._setToken(token)
}

return this.fetchUser()
if (this.options.autoFetchUser) {
await this.fetchUser()
}

return response
}

async setUserToken (tokenValue) {
Expand Down Expand Up @@ -116,5 +121,6 @@ const DEFAULTS = {
tokenRequired: true,
tokenType: 'Bearer',
globalToken: true,
tokenName: 'Authorization'
tokenName: 'Authorization',
autoFetchUser: true
}