From 33a0c7850a3b367445144cb5b8ff251063ff7797 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 13 Dec 2019 13:30:33 +0100 Subject: [PATCH] Logout via AJAX --- client/src/components/App.jsx | 9 +++++++-- client/src/store/actions/logout.js | 6 ++++-- client/src/store/sagas/logout.js | 16 +++++++++++++++- client/src/store/sagas/root.js | 7 ++++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/client/src/components/App.jsx b/client/src/components/App.jsx index 4c2f39f..dc15da6 100644 --- a/client/src/components/App.jsx +++ b/client/src/components/App.jsx @@ -5,6 +5,7 @@ import { ConnectedBoardPage as BoardPage } from './BoardPage/BoardPage'; import { ConnectedEditBoardPage as EditBoardPage } from './BoardPage/EditBoardPage'; import { store } from '../store/store'; import { Provider } from 'react-redux'; +import { logout } from '../store/actions/logout'; export class App extends React.Component { render() { @@ -17,8 +18,8 @@ export class App extends React.Component { { - window.location = "/logout"; - return null; + this.logout(); + return ; }} /> } /> @@ -26,4 +27,8 @@ export class App extends React.Component { ); } + + logout() { + store.dispatch(logout()); + } } \ No newline at end of file diff --git a/client/src/store/actions/logout.js b/client/src/store/actions/logout.js index b54b03a..3537185 100644 --- a/client/src/store/actions/logout.js +++ b/client/src/store/actions/logout.js @@ -1,5 +1,7 @@ -export const LOGOUT = "LOGOUT"; +export const LOGOUT_REQUEST = "LOGOUT_REQUEST"; +export const LOGOUT_SUCCESS = "LOGOUT_SUCCESS"; +export const LOGOUT_FAILURE = "LOGOUT_FAILURE"; export function logout() { - return { type: LOGOUT }; + return { type: LOGOUT_REQUEST }; }; \ No newline at end of file diff --git a/client/src/store/sagas/logout.js b/client/src/store/sagas/logout.js index 6c5dced..26e3fe0 100644 --- a/client/src/store/sagas/logout.js +++ b/client/src/store/sagas/logout.js @@ -1,3 +1,17 @@ +import { call, put } from 'redux-saga/effects'; +import { LOGOUT_FAILURE, LOGOUT_SUCCESS } from '../actions/logout'; + export function* logoutSaga() { - window.location = '/logout'; + try { + yield call(fetch, '/logout', { mode: 'no-cors', credentials: 'include' }); + } catch(err) { + yield put({ type: LOGOUT_FAILURE, error: err }); + return; + } + + yield put({ type: LOGOUT_SUCCESS }); +} + +export function* logoutSuccessSaga() { + window.location.reload(); } \ No newline at end of file diff --git a/client/src/store/sagas/root.js b/client/src/store/sagas/root.js index 8c6b45a..d7a8bff 100644 --- a/client/src/store/sagas/root.js +++ b/client/src/store/sagas/root.js @@ -6,8 +6,8 @@ import { FETCH_ISSUES_REQUEST, ADD_LABEL_REQUEST, REMOVE_LABEL_REQUEST, CREATE_I import { fetchIssuesSaga, addLabelSaga, removeLabelSaga, createIssueSaga } from './issues'; import { FETCH_PROJECTS_REQUEST } from '../actions/projects'; import { fetchProjectsSaga } from './projects'; -import { LOGOUT } from '../actions/logout'; -import { logoutSaga } from './logout'; +import { LOGOUT_REQUEST, LOGOUT_SUCCESS } from '../actions/logout'; +import { logoutSaga, logoutSuccessSaga } from './logout'; import { BUILD_KANBOARD_REQUEST, MOVE_CARD } from '../actions/kanboards'; import { buildKanboardSaga, moveCardSaga, refreshKanboardSaga } from './kanboards'; @@ -25,7 +25,8 @@ export function* rootSaga() { takeEvery(REMOVE_LABEL_REQUEST, removeLabelSaga), takeLatest(CREATE_ISSUE_REQUEST, createIssueSaga), takeLatest(CREATE_ISSUE_SUCCESS, refreshKanboardSaga), - takeLatest(LOGOUT, logoutSaga) + takeLatest(LOGOUT_REQUEST, logoutSaga), + takeLatest(LOGOUT_SUCCESS, logoutSuccessSaga) ]); }