32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
import { call, put } from 'redux-saga/effects';
|
|
import { loginFailure, loginSuccess } from '../../actions/auth.actions';
|
|
import { addMessage } from '../../actions/message.actions';
|
|
import { APIClient } from '../../services/api-client.service.js';
|
|
import { UnauthorizedError } from '../../errors/unauthorized.error.js';
|
|
|
|
export function* loginSaga(action) {
|
|
const client = new APIClient();
|
|
let result;
|
|
try {
|
|
result = yield call(client.login, action.username, action.password);
|
|
} catch(err) {
|
|
if (err instanceof UnauthorizedError) {
|
|
yield put(addMessage('warning', 'Identifiants invalides.'));
|
|
return;
|
|
}
|
|
|
|
yield put(loginFailure(action.username, err));
|
|
yield put(addMessage('danger', 'Une erreur inconnue bloque le fonctionnement normal de l\'application. Veuillez réessayer plus tard.'));
|
|
return;
|
|
}
|
|
|
|
if ('error' in result) {
|
|
yield put(loginFailure(action.username, result.error));
|
|
const message = result.error.message ? result.error.message : result.error.toString();
|
|
yield put(addMessage('danger', message));
|
|
return;
|
|
}
|
|
|
|
yield put(loginSuccess(action.username));
|
|
}
|