From 469f2f85da86d39b9d91a090321316de8ed8d255 Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Sat, 2 Jun 2018 23:35:50 +0900 Subject: [PATCH] feat: add resetOnError (#197) --- docs/options.md | 4 +++- lib/core/auth.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/options.md b/docs/options.md index 3259df120..e6adb4e4d 100644 --- a/docs/options.md +++ b/docs/options.md @@ -103,7 +103,9 @@ See [Extending Auth Plugin](recipes/extend.md) * Default: `false` -If enabled, user will be automatically logged out if any error happens. (For example when token expired) +Either a boolean or a function is accepted. If a function is passed, it will take the same arguments as `onError` handlers and return `Boolean` to inform whether a reset should be performed. + +If enabled, user will be automatically logged out if an error happens. (For example when token expired) ### `rewriteRedirects` diff --git a/lib/core/auth.js b/lib/core/auth.js index a27e4ce40..da2dc6560 100644 --- a/lib/core/auth.js +++ b/lib/core/auth.js @@ -23,6 +23,15 @@ export default class Auth { } async init () { + // Reset on error + if (this.options.resetOnError) { + this.onError((...args) => { + if (typeof (this.options.resetOnError) !== 'function' || this.options.resetOnError(...args)) { + this.reset() + } + }) + } + // Restore strategy this.$storage.syncUniversal('strategy', this.options.defaultStrategy)