From 77d102287338ef44a13f81d218914ade15fec107 Mon Sep 17 00:00:00 2001 From: Gijs van Dam Date: Sun, 14 Apr 2019 21:42:12 +0800 Subject: [PATCH] Add error listeners before Auth.init() In the current setup, if you want to create an error listener, you should create a plugin, and load them through the config with the setting auth.plugins, like so: `plugins: ['~/plugins/auth-error-listener.js']` The plugins are loaded *after* the main Auth plugin itself is loaded. The main plugin takes care of the init. So if an error occurs during init, it's impossible to catch it with a custom error listener. This commit adds a new config option, called `errorListeners`, it's used in the same way as plugins, so: `errorListeners: ['~/utils/auth-error-listener.js']` The plugin itself isn't a nuxt plugin, so it should export a default function: ` export default function(error) { console.log('error listener') console.log(error.response.data) console.log(error.response.status) } ` ErrorListeners will be loaded *before* init. This change should be backwards compatible, since the old method of loading listeners through plugins still works. --- lib/module/defaults.js | 1 + lib/module/plugin.js | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/lib/module/defaults.js b/lib/module/defaults.js index 2be4f71e0..c06ea555e 100644 --- a/lib/module/defaults.js +++ b/lib/module/defaults.js @@ -2,6 +2,7 @@ module.exports = { // -- Error handling -- resetOnError: false, + errorListeners: [], // -- Authorization -- diff --git a/lib/module/plugin.js b/lib/module/plugin.js index ffb025f0c..1fedbf57e 100644 --- a/lib/module/plugin.js +++ b/lib/module/plugin.js @@ -4,6 +4,7 @@ import './middleware' // Active schemes <%= options.uniqueSchemes.map(path =>`import ${'scheme_' + hash(path)} from '${path.replace(/\\/g,'/')}'`).join('\n') %> +<%= options.options.errorListeners.map(path =>`import ${'listener_' + hash(path)} from '${path.replace(/\\/g,'/')}'`).join('\n') %> export default function (ctx, inject) { // Options @@ -15,6 +16,10 @@ export default function (ctx, inject) { // Inject it to nuxt context as $auth inject('auth', $auth) + // Load error listeners + + <%= options.options.errorListeners.map(path =>`$auth.onError(${'listener_' + hash(path)})`).join('\n') %> + // Register strategies <%=