1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

user check on rehydrate

This commit is contained in:
Fedor Katurov 2019-10-08 15:44:11 +07:00
parent eda46bdb9d
commit 2377509d52
4 changed files with 3389 additions and 3390 deletions

6725
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,7 @@ export const API = {
BASE: process.env.API_HOST, BASE: process.env.API_HOST,
USER: { USER: {
LOGIN: '/auth/login', LOGIN: '/auth/login',
ME: '/auth/me', // ME: '/auth/', //
UPLOAD: (target, type) => `/upload/${target}/${type}`, UPLOAD: (target, type) => `/upload/${target}/${type}`,
}, },
NODE: { NODE: {

View file

@ -8,6 +8,7 @@ import {
import { API } from '~/constants/api'; import { API } from '~/constants/api';
import { IResultWithStatus } from '~/redux/types'; import { IResultWithStatus } from '~/redux/types';
import { userLoginTransform } from '~/redux/auth/transforms'; import { userLoginTransform } from '~/redux/auth/transforms';
import { IUser } from './types';
export const apiUserLogin = ({ export const apiUserLogin = ({
username, username,
@ -15,8 +16,15 @@ export const apiUserLogin = ({
}: { }: {
username: string; username: string;
password: string; password: string;
}): Promise<IResultWithStatus<{ token: string; status?: number }>> => api }): Promise<IResultWithStatus<{ token: string; status?: number }>> =>
.post(API.USER.LOGIN, { username, password }) api
.then(resultMiddleware) .post(API.USER.LOGIN, { username, password })
.catch(errorMiddleware) .then(resultMiddleware)
.then(userLoginTransform); .catch(errorMiddleware)
.then(userLoginTransform);
export const apiAuthGetUser = ({ access }): Promise<IResultWithStatus<{ user: IUser }>> =>
api
.get(API.USER.ME, configWithToken(access))
.then(resultMiddleware)
.catch(errorMiddleware);

View file

@ -1,18 +1,20 @@
import { import { call, put, takeLatest, select } from 'redux-saga/effects';
call, put, takeLatest, select,
} from 'redux-saga/effects';
import { SagaIterator } from 'redux-saga';
import { push } from 'connected-react-router'; import { push } from 'connected-react-router';
import { AUTH_USER_ACTIONS } from '~/redux/auth/constants'; import { AUTH_USER_ACTIONS } from '~/redux/auth/constants';
import * as ActionCreators from '~/redux/auth/actions'; import {
import { authSetToken, userSetLoginError, authSetUser } from '~/redux/auth/actions'; authSetToken,
import { apiUserLogin } from '~/redux/auth/api'; userSetLoginError,
authSetUser,
userSendLoginRequest,
} from '~/redux/auth/actions';
import { apiUserLogin, apiAuthGetUser } from '~/redux/auth/api';
import { modalSetShown, modalShowDialog } from '~/redux/modal/actions'; import { modalSetShown, modalShowDialog } from '~/redux/modal/actions';
import { selectToken } from './selectors'; import { selectToken } from './selectors';
import { URLS } from '~/constants/urls'; import { URLS } from '~/constants/urls';
import { DIALOGS } from '../modal/constants'; import { DIALOGS } from '../modal/constants';
import { IResultWithStatus } from '../types'; import { IResultWithStatus } from '../types';
import { IUser } from './types'; import { IUser } from './types';
import { REHYDRATE, RehydrateAction } from 'redux-persist';
export function* reqWrapper(requestAction, props = {}): ReturnType<typeof requestAction> { export function* reqWrapper(requestAction, props = {}): ReturnType<typeof requestAction> {
const access = yield select(selectToken); const access = yield select(selectToken);
@ -25,14 +27,11 @@ export function* reqWrapper(requestAction, props = {}): ReturnType<typeof reques
return result; return result;
} }
return result; return result;
} }
function* sendLoginRequestSaga({ function* sendLoginRequestSaga({ username, password }: ReturnType<typeof userSendLoginRequest>) {
username,
password,
}: ReturnType<typeof ActionCreators.userSendLoginRequest>) {
if (!username || !password) return; if (!username || !password) return;
const { const {
@ -53,7 +52,18 @@ function* sendLoginRequestSaga({
yield put(modalSetShown(false)); yield put(modalSetShown(false));
} }
function* checkUserSaga({ key }: RehydrateAction) {
if (key !== 'auth') return;
const {
data: { user },
}: IResultWithStatus<{ user: IUser }> = yield call(reqWrapper, apiAuthGetUser);
yield put(authSetUser({ ...user, is_user: true }));
}
function* mySaga() { function* mySaga() {
yield takeLatest(REHYDRATE, checkUserSaga);
yield takeLatest(AUTH_USER_ACTIONS.SEND_LOGIN_REQUEST, sendLoginRequestSaga); yield takeLatest(AUTH_USER_ACTIONS.SEND_LOGIN_REQUEST, sendLoginRequestSaga);
} }