mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
user check on rehydrate
This commit is contained in:
parent
eda46bdb9d
commit
2377509d52
4 changed files with 3389 additions and 3390 deletions
6725
package-lock.json
generated
6725
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -4,7 +4,7 @@ export const API = {
|
|||
BASE: process.env.API_HOST,
|
||||
USER: {
|
||||
LOGIN: '/auth/login',
|
||||
ME: '/auth/me', //
|
||||
ME: '/auth/', //
|
||||
UPLOAD: (target, type) => `/upload/${target}/${type}`,
|
||||
},
|
||||
NODE: {
|
||||
|
|
|
@ -8,6 +8,7 @@ import {
|
|||
import { API } from '~/constants/api';
|
||||
import { IResultWithStatus } from '~/redux/types';
|
||||
import { userLoginTransform } from '~/redux/auth/transforms';
|
||||
import { IUser } from './types';
|
||||
|
||||
export const apiUserLogin = ({
|
||||
username,
|
||||
|
@ -15,8 +16,15 @@ export const apiUserLogin = ({
|
|||
}: {
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<IResultWithStatus<{ token: string; status?: number }>> => api
|
||||
.post(API.USER.LOGIN, { username, password })
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware)
|
||||
.then(userLoginTransform);
|
||||
}): Promise<IResultWithStatus<{ token: string; status?: number }>> =>
|
||||
api
|
||||
.post(API.USER.LOGIN, { username, password })
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware)
|
||||
.then(userLoginTransform);
|
||||
|
||||
export const apiAuthGetUser = ({ access }): Promise<IResultWithStatus<{ user: IUser }>> =>
|
||||
api
|
||||
.get(API.USER.ME, configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware);
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
import {
|
||||
call, put, takeLatest, select,
|
||||
} from 'redux-saga/effects';
|
||||
import { SagaIterator } from 'redux-saga';
|
||||
import { call, put, takeLatest, select } from 'redux-saga/effects';
|
||||
import { push } from 'connected-react-router';
|
||||
import { AUTH_USER_ACTIONS } from '~/redux/auth/constants';
|
||||
import * as ActionCreators from '~/redux/auth/actions';
|
||||
import { authSetToken, userSetLoginError, authSetUser } from '~/redux/auth/actions';
|
||||
import { apiUserLogin } from '~/redux/auth/api';
|
||||
import {
|
||||
authSetToken,
|
||||
userSetLoginError,
|
||||
authSetUser,
|
||||
userSendLoginRequest,
|
||||
} from '~/redux/auth/actions';
|
||||
import { apiUserLogin, apiAuthGetUser } from '~/redux/auth/api';
|
||||
import { modalSetShown, modalShowDialog } from '~/redux/modal/actions';
|
||||
import { selectToken } from './selectors';
|
||||
import { URLS } from '~/constants/urls';
|
||||
import { DIALOGS } from '../modal/constants';
|
||||
import { IResultWithStatus } from '../types';
|
||||
import { IUser } from './types';
|
||||
import { REHYDRATE, RehydrateAction } from 'redux-persist';
|
||||
|
||||
export function* reqWrapper(requestAction, props = {}): ReturnType<typeof requestAction> {
|
||||
const access = yield select(selectToken);
|
||||
|
@ -25,14 +27,11 @@ export function* reqWrapper(requestAction, props = {}): ReturnType<typeof reques
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function* sendLoginRequestSaga({
|
||||
username,
|
||||
password,
|
||||
}: ReturnType<typeof ActionCreators.userSendLoginRequest>) {
|
||||
function* sendLoginRequestSaga({ username, password }: ReturnType<typeof userSendLoginRequest>) {
|
||||
if (!username || !password) return;
|
||||
|
||||
const {
|
||||
|
@ -53,7 +52,18 @@ function* sendLoginRequestSaga({
|
|||
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() {
|
||||
yield takeLatest(REHYDRATE, checkUserSaga);
|
||||
yield takeLatest(AUTH_USER_ACTIONS.SEND_LOGIN_REQUEST, sendLoginRequestSaga);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue