diff --git a/frontend/src/errors/unauthorized.error.js b/frontend/src/errors/unauthorized.error.js new file mode 100644 index 0000000..6562173 --- /dev/null +++ b/frontend/src/errors/unauthorized.error.js @@ -0,0 +1,6 @@ +export class UnauthorizedError extends Error { + constructor() { + super("Unauthorized"); + Error.captureStackTrace(this, UnauthorizedError); + } +} diff --git a/frontend/src/sagas/auth.sagas.js b/frontend/src/sagas/auth.sagas.js index 3bd9d08..da7d2e2 100644 --- a/frontend/src/sagas/auth.sagas.js +++ b/frontend/src/sagas/auth.sagas.js @@ -27,6 +27,7 @@ export function* loginSaga(action) { export function* logoutSaga(action) { + const client = new APIClient(); let result; try { result = yield call(client.logout); diff --git a/frontend/src/sagas/failure.sagas.js b/frontend/src/sagas/failure.sagas.js new file mode 100644 index 0000000..2add5d5 --- /dev/null +++ b/frontend/src/sagas/failure.sagas.js @@ -0,0 +1,12 @@ +import { put } from 'redux-saga/effects'; +import { UnauthorizedError } from '../errors/unauthorized.error.js'; +import { addMessage } from '../actions/message.actions.js'; +import { logout } from '../actions/auth.actions.js'; + +export function* failureActionSaga({ error }) { + console.error(error); + if (error instanceof UnauthorizedError) { + yield put(addMessage('danger', 'Vous avez été déconnecté.')); + yield put(logout()); + } +} diff --git a/frontend/src/sagas/root.js b/frontend/src/sagas/root.js index 845dc31..3576572 100644 --- a/frontend/src/sagas/root.js +++ b/frontend/src/sagas/root.js @@ -3,6 +3,7 @@ import { LOGIN_REQUEST, LOGOUT_REQUEST } from '../actions/auth.actions'; import { loginSaga, logoutSaga } from './auth.sagas'; import { PROJECT_USER_LIST, PROJECT_LIST } from '../actions/project'; import { projectUserListSaga, projectListSaga } from './project'; +import { failureActionSaga } from './failure.sagas'; export default function* rootSaga() { yield all([ @@ -10,5 +11,6 @@ export default function* rootSaga() { takeLatest(LOGOUT_REQUEST, logoutSaga), takeLatest(PROJECT_USER_LIST, projectUserListSaga), takeLatest(PROJECT_LIST, projectListSaga), + takeLatest(action => /_FAILURE$/g.test(action.type), failureActionSaga), ]); } diff --git a/frontend/src/services/api-client.service.js b/frontend/src/services/api-client.service.js index fb200c7..058bc70 100644 --- a/frontend/src/services/api-client.service.js +++ b/frontend/src/services/api-client.service.js @@ -1,7 +1,10 @@ +import { UnauthorizedError } from '../errors/unauthorized.error.js'; + +export const UnauthorizedStatusCode = 401; export class APIClient { constructor(baseURL = 'http://localhost:8001/api/v1') { - this.baseURL = baseURL; + this.baseURL = baseURL; this.login = this.login.bind(this); this.logout = this.logout.bind(this); this.me = this.me.bind(this); @@ -47,6 +50,12 @@ export class APIClient { credentials: 'include', body: JSON.stringify(body), }) + .then(res => { + if (res.status === UnauthorizedStatusCode) { + throw new UnauthorizedError(); + } + return res; + }) .then(res => res.json()) }