Skip to content

Commit

Permalink
feat: $auth.onError
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa authored and pi0 committed Feb 2, 2018
1 parent d4da740 commit 151868a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
32 changes: 21 additions & 11 deletions lib/auth.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Cookie from 'cookie'
import Cookies from 'js-cookie'
import Vue from 'vue'
import Hookable from 'hable'

export default class Auth extends Hookable {
export default class Auth {
constructor (ctx, options) {
super()

this.ctx = ctx
this.app = ctx.app
this.options = options

// Error listeners
this._errorListeners = []

// Keep token out of the store for security reasons
Vue.set(this, 'token', null)

Expand Down Expand Up @@ -42,7 +42,7 @@ export default class Auth extends Hookable {
}

_resetOnError () {
this.hook('error', () => {
this.onError(() => {
this.reset()
})
}
Expand All @@ -60,6 +60,16 @@ export default class Auth extends Hookable {
)
}

onError (listener) {
this._errorListeners.push(listener)
}

_onError () {
for (let fn of this._errorListeners) {
fn.apply(this, arguments)
}
}

get $axios () {
if (!this.app.$axios) {
throw new Error('$axios is not available')
Expand Down Expand Up @@ -174,7 +184,7 @@ export default class Auth extends Hookable {
return cookies[name]
}

async _fetch (name, endpoint) {
async _request (name, endpoint) {
const defaults = this.options.endpoints[name]
if (!defaults) {
return
Expand All @@ -185,13 +195,13 @@ export default class Auth extends Hookable {
Object.assign({}, defaults, endpoint)
)
return data
} catch (error) {
await this.callHook('error', { name, endpoint, error })
} catch (err) {
this._onError({ name, err, endpoint })
}
}

async login (endpoint) {
const data = await this._fetch('login', endpoint)
const data = await this._request('login', endpoint)
if (!data) {
return
}
Expand All @@ -213,7 +223,7 @@ export default class Auth extends Hookable {
return
}

const data = await this._fetch('user', endpoint)
const data = await this._request('user', endpoint)
if (!data) {
return
}
Expand All @@ -223,7 +233,7 @@ export default class Auth extends Hookable {
}

async logout (endpoint) {
await this._fetch('logout', endpoint)
await this._request('logout', endpoint)

this.reset()
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"dependencies": {
"@nuxtjs/axios": "^5.0.0-rc.2",
"cookie": "^0.3.1",
"hable": "^0.0.7",
"js-cookie": "^2.2.0",
"lodash": "^4.17.4"
},
Expand Down
10 changes: 2 additions & 8 deletions test/fixture/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

<script>
export default {
data() {
return {
errors: []
}
},
created() {
this.$auth.hook('error', (err => {
this.$auth.onError(err => {
console.error(err)
this.errors.push(err)
}))
})
},
computed: {
state() {
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3192,12 +3192,6 @@ gzip-size@^3.0.0:
dependencies:
duplexer "^0.1.1"

hable@^0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/hable/-/hable-0.0.7.tgz#9f75bb220d91e02baad5604b9143105e8d8fb8e9"
dependencies:
items-promise "^1.0.0"

handlebars@^4.0.2, handlebars@^4.0.3:
version "4.0.11"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.11.tgz#630a35dfe0294bc281edae6ffc5d329fc7982dcc"
Expand Down Expand Up @@ -3930,10 +3924,6 @@ istanbul-reports@^1.1.3:
dependencies:
handlebars "^4.0.3"

items-promise@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/items-promise/-/items-promise-1.0.0.tgz#a71ff03063078eb82db468f1e2f12df4da34dc01"

jest-changed-files@^22.1.4:
version "22.1.4"
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-22.1.4.tgz#1f7844bcb739dec07e5899a633c0cb6d5069834e"
Expand Down

0 comments on commit 151868a

Please sign in to comment.