12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { actionIsLoading } from '../../loading/action';
- import {
- actionLogInSuccess,
- actionLogInReject,
- actionLogOutSuccess,
- actionLogOutReject,
- } from '../action';
- import { loginGQL, registerGQL } from '../../../api-data';
- const asyncLogin = (login, password) => async dispatch => {
- try {
- dispatch(actionIsLoading(true));
- const token = await loginGQL(login, password);
- if (!token) throw new Error('Wrong credentials');
- localStorage.token = token;
- dispatch(actionLogInSuccess({ login, token }));
- } catch (e) {
- dispatch(actionLogInReject());
- } finally {
- dispatch(actionIsLoading(false));
- }
- };
- const asyncLogout = () => async dispatch => {
- try {
- dispatch(actionIsLoading(true));
- dispatch(actionLogOutSuccess({ login: '', token: '' }));
- localStorage.token = null;
- } catch (e) {
- dispatch(actionLogOutReject());
- } finally {
- dispatch(actionIsLoading(false));
- }
- };
- const asyncCreateUser = (login, password) => async dispatch => {
- try {
- dispatch(actionIsLoading(true));
- await registerGQL(login, password);
- const token = await loginGQL(login, password);
- if (!token) throw new Error('Server error');
- dispatch(actionLogInSuccess({ login, token }));
- } catch (e) {
- console.error('Credentials have already used', e);
- } finally {
- dispatch(actionIsLoading(false));
- }
- };
- export { asyncLogin, asyncLogout, asyncCreateUser };
|