Add logout commit for 401 response

Change the authentication logout from router to api interceptor,
so that if a user accidentally navigates to login page by clicking
the browser back button, they aren't automatically logged out.
Logouts would occur when hitting a 401 response or if the user
clicks the logout button from app header.

Signed-off-by: Yoshie Muranaka <yoshiemuranaka@gmail.com>
Change-Id: I6290070b63e724b75b3ac2fc39b3c7e814fbfc3e
diff --git a/src/router/index.js b/src/router/index.js
index 33743fe..c3d4439 100644
--- a/src/router/index.js
+++ b/src/router/index.js
@@ -98,9 +98,6 @@
 });
 
 router.beforeEach((to, from, next) => {
-  // Commit logout to remove XSRF-TOKEN cookie when
-  // redirected to login page (eg 401 response)
-  if (to.name === 'login') store.commit('authentication/logout');
   if (to.matched.some(record => record.meta.requiresAuth)) {
     if (store.getters['authentication/isLoggedIn']) {
       next();
diff --git a/src/store/api.js b/src/store/api.js
index 24a38e4..c8f1eda 100644
--- a/src/store/api.js
+++ b/src/store/api.js
@@ -1,5 +1,6 @@
 import Axios from 'axios';
 import router from '../router';
+import store from '@/store';
 
 const api = Axios.create({
   withCredentials: true
@@ -12,6 +13,8 @@
   if (response.status == 401) {
     if (response.config.url != '/login') {
       window.location = '/login';
+      // Commit logout to remove XSRF-TOKEN cookie
+      store.commit('authentication/logout');
     }
   }