From 1d93e6c06268afa948c41b285acb191d31343b60 Mon Sep 17 00:00:00 2001
From: Albin Henriksson <albhe428@student.liu.se>
Date: Mon, 19 Apr 2021 14:32:50 +0200
Subject: [PATCH] Call logout api when logging out

---
 client/src/actions/user.test.ts              |  3 +++
 client/src/actions/user.ts                   | 14 ++++++++------
 client/src/utils/checkAuthentication.test.ts | 14 ++++++++++++--
 client/src/utils/checkAuthentication.ts      |  6 +++---
 4 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/client/src/actions/user.test.ts b/client/src/actions/user.test.ts
index 156b1718..6ca594fa 100644
--- a/client/src/actions/user.test.ts
+++ b/client/src/actions/user.test.ts
@@ -39,6 +39,9 @@ it('dispatches correct actions when logging in user', async () => {
 })
 
 it('dispatches correct action when logging out user', async () => {
+  ;(mockedAxios.post as jest.Mock).mockImplementation((path: string, params?: any) => {
+    return Promise.resolve({ data: {} })
+  })
   const store = mockStore({})
   await logoutUser()(store.dispatch)
   expect(store.getActions()).toEqual([{ type: Types.SET_UNAUTHENTICATED }])
diff --git a/client/src/actions/user.ts b/client/src/actions/user.ts
index 30374cca..e14ea4c0 100644
--- a/client/src/actions/user.ts
+++ b/client/src/actions/user.ts
@@ -40,11 +40,13 @@ export const getUserData = () => async (dispatch: AppDispatch) => {
     })
 }
 
-export const logoutUser = () => (dispatch: AppDispatch) => {
-  localStorage.removeItem('token')
-  delete axios.defaults.headers.common['Authorization']
-  dispatch({
-    type: Types.SET_UNAUTHENTICATED,
+export const logoutUser = () => async (dispatch: AppDispatch) => {
+  await axios.post('/auth/logout').then(() => {
+    localStorage.removeItem('token')
+    delete axios.defaults.headers.common['Authorization']
+    dispatch({
+      type: Types.SET_UNAUTHENTICATED,
+    })
+    window.location.href = '/' //redirect to login page
   })
-  window.location.href = '/' //redirect to login page
 }
diff --git a/client/src/utils/checkAuthentication.test.ts b/client/src/utils/checkAuthentication.test.ts
index 901f331d..6d12e1fc 100644
--- a/client/src/utils/checkAuthentication.test.ts
+++ b/client/src/utils/checkAuthentication.test.ts
@@ -13,7 +13,9 @@ it('dispatches correct actions when auth token is ok', async () => {
   ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => {
     return Promise.resolve(userRes)
   })
-
+  ;(mockedAxios.post as jest.Mock).mockImplementation((path: string, params?: any) => {
+    return Promise.resolve({ data: {} })
+  })
   const spy = jest.spyOn(store, 'dispatch')
   const testToken =
     'Bearer eyJ0eXAiOiJeyJ0eXAiOiJKV1QeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSceyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSceyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ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dG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxScKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSc'
@@ -30,7 +32,9 @@ it('dispatches correct actions when getting user data fails', async () => {
   ;(mockedAxios.get as jest.Mock).mockImplementation((path: string, params?: any) => {
     return Promise.reject(new Error('failed getting user data'))
   })
-
+  ;(mockedAxios.post as jest.Mock).mockImplementation((path: string, params?: any) => {
+    return Promise.resolve({ data: {} })
+  })
   const spy = jest.spyOn(store, 'dispatch')
   const testToken =
     'Bearer eyJ0eXAiOiJeyJ0eXAiOiJKV1QeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSceyJ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iUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSciLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxScKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDQ5MzksImV4cCI6MzI1MTI1MjU3NTcsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJHaXZlbk5hbWUiOiJKb2hubnkiLCJTdXJuYW1lIjoiUm9ja2V0IiwiRW1haWwiOiJqcm9ja2V0QGV4YW1wbGUuY29tIiwiUm9sZSI6WyJNYW5hZ2VyIiwiUHJvamVjdCBBZG1pbmlzdHJhdG9yIl19.DrOOcCo5jMR-SNERE0uRp_kolQ2HjX8-hHXYEMnIxSc'
@@ -43,12 +47,18 @@ it('dispatches correct actions when getting user data fails', async () => {
 })
 
 it('dispatches no actions when no token exists', async () => {
+  ;(mockedAxios.post as jest.Mock).mockImplementation((path: string, params?: any) => {
+    return Promise.resolve({ data: {} })
+  })
   const spy = jest.spyOn(store, 'dispatch')
   await CheckAuthentication()
   expect(spy).not.toBeCalled()
 })
 
 it('dispatches correct actions when token is expired', async () => {
+  ;(mockedAxios.post as jest.Mock).mockImplementation((path: string, params?: any) => {
+    return Promise.resolve({ data: {} })
+  })
   const testToken =
     'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MTgzMDY1MTUsImV4cCI6MTU4Njc3MDUxNSwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJFbWFpbCI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJSb2xlIjpbIk1hbmFnZXIiLCJQcm9qZWN0IEFkbWluaXN0cmF0b3IiXX0.R5-oWGGumd-YWPoKyziJmVB8SdX6B9SsV6m7novIfgg'
   localStorage.setItem('token', testToken)
diff --git a/client/src/utils/checkAuthentication.ts b/client/src/utils/checkAuthentication.ts
index 23178154..a782ea10 100644
--- a/client/src/utils/checkAuthentication.ts
+++ b/client/src/utils/checkAuthentication.ts
@@ -4,8 +4,8 @@ import Types from '../actions/types'
 import { logoutUser } from '../actions/user'
 import store from '../store'
 
-const UnAuthorized = () => {
-  logoutUser()(store.dispatch)
+const UnAuthorized = async () => {
+  await logoutUser()(store.dispatch)
 }
 
 export const CheckAuthentication = async () => {
@@ -29,7 +29,7 @@ export const CheckAuthentication = async () => {
           UnAuthorized()
         })
     } else {
-      UnAuthorized()
+      await UnAuthorized()
     }
   }
 }
-- 
GitLab