diff --git a/src/constants/api.ts b/src/constants/api.ts index 0ceacee3..ff0f9733 100644 --- a/src/constants/api.ts +++ b/src/constants/api.ts @@ -2,5 +2,6 @@ export const API = { BASE: 'http://localhost:3333', USER: { LOGIN: '/auth/login', + ME: '/auth/me', } }; diff --git a/src/redux/auth/api.ts b/src/redux/auth/api.ts index dd0385b9..664bbc1a 100644 --- a/src/redux/auth/api.ts +++ b/src/redux/auth/api.ts @@ -1,15 +1,22 @@ import {api, authMiddleware, errorMiddleware, resultMiddleware} from "~/utils/api"; import { API } from "~/constants/api"; -import { IApiUser } from "~/redux/auth/constants"; 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 = ( { username, password }: { username: string, password: string } -): Promise> => ( +): Promise> => ( api.post(API.USER.LOGIN, { username, password }) .then(resultMiddleware) .catch(errorMiddleware) .then(userLoginTransform) ); + +export const getAuthSelf = (): Promise> => ( + api.get(API.USER.ME) + .then(resultMiddleware) + .catch(errorMiddleware) + .then(authMeTransform) +); diff --git a/src/redux/auth/sagas.ts b/src/redux/auth/sagas.ts index 7b59aa3e..2c2cab24 100644 --- a/src/redux/auth/sagas.ts +++ b/src/redux/auth/sagas.ts @@ -3,7 +3,7 @@ import { SagaIterator } from 'redux-saga'; import {AUTH_USER_ACTIONS} from "~/redux/auth/constants"; import * as ActionCreators 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"; function* sendLoginRequestSaga({ username, password }: ReturnType): SagaIterator { @@ -17,6 +17,8 @@ function* sendLoginRequestSaga({ username, password }: ReturnType): IResultWithStatus => { + switch(true) { + case status === 401: + return { status, data, error: 'Пользователь не авторизован' }; + + case status === 200: + return { status, data, error: null }; + + default: + return { status, data, error: error || 'Неизвестная ошибка' }; + } +};