mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
get self api fn
This commit is contained in:
parent
6f9f255abf
commit
e119ae5fc4
4 changed files with 28 additions and 5 deletions
|
@ -2,5 +2,6 @@ export const API = {
|
||||||
BASE: 'http://localhost:3333',
|
BASE: 'http://localhost:3333',
|
||||||
USER: {
|
USER: {
|
||||||
LOGIN: '/auth/login',
|
LOGIN: '/auth/login',
|
||||||
|
ME: '/auth/me',
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,15 +1,22 @@
|
||||||
import {api, authMiddleware, errorMiddleware, resultMiddleware} from "~/utils/api";
|
import {api, authMiddleware, errorMiddleware, resultMiddleware} from "~/utils/api";
|
||||||
import { API } from "~/constants/api";
|
import { API } from "~/constants/api";
|
||||||
import { IApiUser } from "~/redux/auth/constants";
|
|
||||||
import {IResultWithStatus} from "~/redux/types";
|
import {IResultWithStatus} from "~/redux/types";
|
||||||
import {userLoginTransform} from "~/redux/auth/transforms";
|
import {authMeTransform, userLoginTransform} from "~/redux/auth/transforms";
|
||||||
|
import {IUser} from "~/redux/auth/types";
|
||||||
|
|
||||||
export const apiUserLogin = (
|
export const apiUserLogin = (
|
||||||
{ username, password }:
|
{ username, password }:
|
||||||
{ username: string, password: string }
|
{ username: string, password: string }
|
||||||
): Promise<IResultWithStatus<{ token: string, status?: number, user?: IApiUser }>> => (
|
): Promise<IResultWithStatus<{ token: string, status?: number }>> => (
|
||||||
api.post(API.USER.LOGIN, { username, password })
|
api.post(API.USER.LOGIN, { username, password })
|
||||||
.then(resultMiddleware)
|
.then(resultMiddleware)
|
||||||
.catch(errorMiddleware)
|
.catch(errorMiddleware)
|
||||||
.then(userLoginTransform)
|
.then(userLoginTransform)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const getAuthSelf = (): Promise<IResultWithStatus<{ user: IUser }>> => (
|
||||||
|
api.get(API.USER.ME)
|
||||||
|
.then(resultMiddleware)
|
||||||
|
.catch(errorMiddleware)
|
||||||
|
.then(authMeTransform)
|
||||||
|
);
|
||||||
|
|
|
@ -3,7 +3,7 @@ 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 * as ActionCreators from '~/redux/auth/actions';
|
||||||
import {authSetToken, userSetLoginError} from "~/redux/auth/actions";
|
import {authSetToken, userSetLoginError} from "~/redux/auth/actions";
|
||||||
import {apiUserLogin} from "~/redux/auth/api";
|
import {apiUserLogin, getAuthSelf} from "~/redux/auth/api";
|
||||||
import {modalSetShown} from "~/redux/modal/actions";
|
import {modalSetShown} from "~/redux/modal/actions";
|
||||||
|
|
||||||
function* sendLoginRequestSaga({ username, password }: ReturnType<typeof ActionCreators.userSendLoginRequest>): SagaIterator {
|
function* sendLoginRequestSaga({ username, password }: ReturnType<typeof ActionCreators.userSendLoginRequest>): SagaIterator {
|
||||||
|
@ -17,6 +17,8 @@ function* sendLoginRequestSaga({ username, password }: ReturnType<typeof ActionC
|
||||||
|
|
||||||
yield put(authSetToken({ access, refresh }));
|
yield put(authSetToken({ access, refresh }));
|
||||||
|
|
||||||
|
const info = yield call(getAuthSelf); // todo: reqWrapper here
|
||||||
|
|
||||||
// todo: get /auth/me
|
// todo: get /auth/me
|
||||||
|
|
||||||
yield put(modalSetShown(false));
|
yield put(modalSetShown(false));
|
||||||
|
|
|
@ -11,4 +11,17 @@ export const userLoginTransform = ({ status, data,error }: IResultWithStatus<any
|
||||||
default:
|
default:
|
||||||
return { status, data, error: error || 'Неизвестная ошибка' };
|
return { status, data, error: error || 'Неизвестная ошибка' };
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
export const authMeTransform = ({ status, data,error }: IResultWithStatus<any>): IResultWithStatus<any> => {
|
||||||
|
switch(true) {
|
||||||
|
case status === 401:
|
||||||
|
return { status, data, error: 'Пользователь не авторизован' };
|
||||||
|
|
||||||
|
case status === 200:
|
||||||
|
return { status, data, error: null };
|
||||||
|
|
||||||
|
default:
|
||||||
|
return { status, data, error: error || 'Неизвестная ошибка' };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue