1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00
This commit is contained in:
muerwre 2019-08-06 18:21:19 +07:00
parent 56d714d655
commit e0bba90d2e
12 changed files with 264 additions and 89 deletions

View file

@ -1,26 +1,43 @@
import {call, put, takeLatest } from 'redux-saga/effects';
import { call, put, takeLatest, select } from 'redux-saga/effects';
import { SagaIterator } from 'redux-saga';
import {AUTH_USER_ACTIONS} from "~/redux/auth/constants";
import { AUTH_USER_ACTIONS } from "~/redux/auth/constants";
import * as ActionCreators from '~/redux/auth/actions';
import {authSetToken, userSetLoginError} from "~/redux/auth/actions";
import {apiUserLogin, getAuthSelf} from "~/redux/auth/api";
import {modalSetShown} from "~/redux/modal/actions";
import { authSetToken, userSetLoginError, authSetUser } from "~/redux/auth/actions";
import { apiUserLogin } from "~/redux/auth/api";
import { modalSetShown, modalShowDialog } from "~/redux/modal/actions";
import { selectToken } from './selectors';
import { push } from 'connected-react-router';
import { URLS } from '~/constants/urls';
import { DIALOGS } from '../modal/constants';
import { IResultWithStatus } from '../types';
import { IToken, IUser } from './types';
export function* reqWrapper(requestAction, props = {}): ReturnType<typeof requestAction> {
const { access } = yield select(selectToken);
const result = yield call(requestAction, { access, ...props });
if (result && result.status === 401) {
yield put(push(URLS.BASE));
yield put(modalShowDialog(DIALOGS.LOGIN));
return result;
}
return result;
}
function* sendLoginRequestSaga({ username, password }: ReturnType<typeof ActionCreators.userSendLoginRequest>): SagaIterator {
if (!username || !password) return;
const { error, data: { access, refresh }} = yield call(apiUserLogin, { username, password });
const { error, data: { token, user } }: IResultWithStatus<{ token: string, user: IUser }> = yield call(apiUserLogin, { username, password });
console.log({ access, refresh, error });
console.log({ token, error });
if (error) return yield put(userSetLoginError(error));
yield put(authSetToken({ access, refresh }));
const info = yield call(getAuthSelf); // todo: reqWrapper here
// todo: get /auth/me
yield put(authSetToken(token));
yield put(authSetUser(user));
yield put(modalSetShown(false));
}