Ajout page d'authentification
This commit is contained in:
33
frontend/src/sagas/auth.sagas.js
Normal file
33
frontend/src/sagas/auth.sagas.js
Normal file
@ -0,0 +1,33 @@
|
||||
import { call, put } from 'redux-saga/effects';
|
||||
import { loginFailure, loginSuccess } from '../actions/auth.actions';
|
||||
|
||||
export function* loginSaga(action) {
|
||||
let result;
|
||||
try {
|
||||
result = yield call(doLogin, action.username, action.password);
|
||||
} catch(err) {
|
||||
yield put(loginFailure(action.username, err));
|
||||
}
|
||||
|
||||
if ('error' in result) {
|
||||
yield put(loginFailure(action.username, result.error));
|
||||
return
|
||||
}
|
||||
|
||||
yield put(loginSuccess(action.username));
|
||||
}
|
||||
|
||||
function doLogin(username, password) {
|
||||
return fetch('http://localhost:8001/api/v1/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
username: username,
|
||||
password: password
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
mode: 'cors',
|
||||
credentials: 'include'
|
||||
}).then(res => res.json())
|
||||
}
|
Reference in New Issue
Block a user