mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 21:06:42 +07:00
auth
This commit is contained in:
parent
56d714d655
commit
e0bba90d2e
12 changed files with 264 additions and 89 deletions
|
@ -7,8 +7,7 @@ import {
|
|||
} from '~/utils/api';
|
||||
import { API } from '~/constants/api';
|
||||
import { IResultWithStatus } from '~/redux/types';
|
||||
import { authMeTransform, userLoginTransform } from '~/redux/auth/transforms';
|
||||
import { IUser } from '~/redux/auth/types';
|
||||
import { userLoginTransform } from '~/redux/auth/transforms';
|
||||
|
||||
export const apiUserLogin = ({
|
||||
username,
|
||||
|
@ -22,10 +21,3 @@ export const apiUserLogin = ({
|
|||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware)
|
||||
.then(userLoginTransform);
|
||||
|
||||
export const getAuthSelf = ({ access }): Promise<IResultWithStatus<{ user: IUser }>> =>
|
||||
api
|
||||
.get(API.USER.ME, configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware)
|
||||
.then(authMeTransform);
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
import {EMPTY_TOKEN, EMPTY_USER, AUTH_USER_ACTIONS} from "~/redux/auth/constants";
|
||||
import { EMPTY_USER } from "~/redux/auth/constants";
|
||||
import { createReducer } from "~/utils/reducer";
|
||||
import {IAuthState} from "~/redux/auth/types";
|
||||
import {AUTH_USER_HANDLERS} from "~/redux/auth/handlers";
|
||||
import { IAuthState } from "~/redux/auth/types";
|
||||
import { AUTH_USER_HANDLERS } from "~/redux/auth/handlers";
|
||||
|
||||
const HANDLERS = {
|
||||
...AUTH_USER_HANDLERS,
|
||||
};
|
||||
|
||||
const INITIAL_STATE: IAuthState = {
|
||||
token: { ...EMPTY_TOKEN },
|
||||
token: null,
|
||||
user: { ...EMPTY_USER },
|
||||
login: {
|
||||
error: null,
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import {IState} from "~/redux/store";
|
||||
import { IState } from "~/redux/store";
|
||||
|
||||
export const selectUser = (state: IState): IState['auth']['user'] => state.auth.user;
|
||||
export const selectToken = (state: IState): IState['auth']['token'] => state.auth.token;
|
||||
export const selectAuthLogin = (state: IState): IState['auth']['login'] => state.auth.login;
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import {IResultWithStatus} from "~/redux/types";
|
||||
import { IResultWithStatus } from "~/redux/types";
|
||||
import { HTTP_RESPONSES } from "~/utils/api";
|
||||
|
||||
export const userLoginTransform = ({ status, data,error }: IResultWithStatus<any>): IResultWithStatus<any> => {
|
||||
switch(true) {
|
||||
case status === 401 || !data.access || data.refresh:
|
||||
export const userLoginTransform = ({ status, data, error }: IResultWithStatus<any>): IResultWithStatus<any> => {
|
||||
switch (true) {
|
||||
case (status === HTTP_RESPONSES.UNAUTHORIZED || !data.token) && status !== HTTP_RESPONSES.CONNECTION_REFUSED:
|
||||
return { status, data, error: 'Пользователь не найден' };
|
||||
|
||||
case status === 200:
|
||||
|
@ -11,17 +12,4 @@ export const userLoginTransform = ({ status, data,error }: IResultWithStatus<any
|
|||
default:
|
||||
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 || 'Неизвестная ошибка' };
|
||||
}
|
||||
};
|
||||
};
|
|
@ -17,7 +17,7 @@ export interface IUser {
|
|||
|
||||
export type IAuthState = Readonly<{
|
||||
user: IUser;
|
||||
token: IToken;
|
||||
token: string;
|
||||
|
||||
login: {
|
||||
error: string;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue