From a11ba63d83adabb8ebecafa5e879ccc4c51a523e Mon Sep 17 00:00:00 2001 From: Jacques Yakoub Date: Fri, 24 Apr 2020 22:44:28 +0200 Subject: [PATCH 1/2] feat: Force refresh token with we got a 401 (WIP) Keep it simple ^^ : https://gist.github.com/gnumarcelo/98b9016679ffe9136b56eea61b9e520f --- plugins/axios.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/axios.ts b/plugins/axios.ts index 089b7a6..01321c0 100644 --- a/plugins/axios.ts +++ b/plugins/axios.ts @@ -16,13 +16,13 @@ const axios: Plugin = ({$axios, redirect, $auth, app}) => { return response }, async (error) => { - if (error.response.status === 401 && app.router && app.router.currentRoute.path !== '/login') { - - if($auth.loggedIn) { - await $auth.logout(); - } - - redirect(401, "/login"); + const status = parseInt(error.response && error.response.status); + const shouldRefresh = [401, 403].includes(code); + if (shouldRefresh) { + await $auth.logout(); + } + if (shouldRefresh && app.router && app.router.currentRoute.path !== '/login') { + redirect(status, "/login"); } else { return Promise.reject(error); } From 19286784ade4ff8310b5b2ec78e0128066334a60 Mon Sep 17 00:00:00 2001 From: Jacques Yakoub Date: Fri, 24 Apr 2020 22:47:35 +0200 Subject: [PATCH 2/2] Update axios.ts --- plugins/axios.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/axios.ts b/plugins/axios.ts index 01321c0..6c3aa47 100644 --- a/plugins/axios.ts +++ b/plugins/axios.ts @@ -17,7 +17,7 @@ const axios: Plugin = ({$axios, redirect, $auth, app}) => { }, async (error) => { const status = parseInt(error.response && error.response.status); - const shouldRefresh = [401, 403].includes(code); + const shouldRefresh = [401, 403].includes(status); if (shouldRefresh) { await $auth.logout(); }