unknown 2 lat temu
rodzic
commit
c6fd6d7ede

Plik diff jest za duży
+ 1 - 1
.eslintcache


+ 8 - 0
src/App.tsx

@@ -1,8 +1,12 @@
 import { lazy, Suspense } from 'react';
 import { BrowserRouter, Switch } from 'react-router-dom';
 import { ToastContainer } from 'react-toastify';
+import { useEffect } from 'react';
+import { useSelector } from 'react-redux';
 
 import s from './App.module.css';
+import { getToken } from './redux/authorization/selector'
+import { setToken } from './api-data'
 import PrivateRoute from './components/Routes/PrivateRoute/PrivateRoute';
 import PublicRoute from './components/Routes/PublicRoute/PublicRoute';
 import Loader from './components/Loader/Loader';
@@ -19,6 +23,10 @@ const AuthPage = lazy(
 
 
 function App() {
+  const token = useSelector(getToken)
+  useEffect(() => {
+    if (localStorage.isChecked&&token) setToken.set(token)
+  }, [token])
 
   return (
     <div className={s.appWrapper}>

+ 9 - 12
src/api-data/index.ts

@@ -1,6 +1,6 @@
 import { toast } from 'react-toastify';
 const axios = require('axios');
-axios.defaults.baseURL = 'http://localhost:3000';
+axios.defaults.baseURL = 'http://localhost:3000/api';
 
 const error = (message:string) =>
   toast.error(`🔥 ${message}!`, {
@@ -39,7 +39,7 @@ const authorizeUser = async (number:string,country:string):Promise<string | unde
     success(`check ${number}`);
     return data
   } catch (e) {
-    error('bad request');
+    error('BAD REQUEST');
   }
 };
 
@@ -53,24 +53,21 @@ const loginUser = async <T>(number:string,code:string):Promise<T | undefined> =>
   }
 };
 
-const updateCredentials = async <T>(name: string,lastName: string):Promise<T | undefined> => {
+const updateCredentials = async (name: string,lastName: string):Promise<void> => {
   try {
-    const { data } = await axios.patch('/users/current', { name, lastName });
+    await axios.patch('/users/current', { name, lastName });
     success(`CREDENTIALS UPDATED`);
-    console.log(data)
-    return data;
   } catch (e) {
-    console.log(e)
-    error('bad request');
+    error('BAD REQUEST');
   }
 };
-const updateUserAvatar = async <T>(avatarFile: object):Promise<T | undefined> => {
+
+const updateUserAvatar = async (file: object):Promise<void> => {
   try {
-    const { data } = await axios.patch('/users/avatars', avatarFile);
+    await axios.patch('/users/avatar', file);
     success(`AVATAR UPDATED`);
-    return data;
   } catch (e) {
-    error('bad request');
+    error('BAD REQUEST');
   } 
 };
 

+ 2 - 2
src/components/AuthPage/index.tsx

@@ -11,11 +11,11 @@ import {asyncLogin} from '../../redux/authorization/operations'
 
 const AuthPage = () => {
   const [isQR, setIsQR] = useState<boolean>(false)
-  const [isCode, setIsCode] = useState<string>('123')
+  const [isCode, setIsCode] = useState<string>('')
   const [isChecked, setIsChecked] = useState<boolean>(true);
   const [isReg, setIsReg] = useState<boolean>(false);
   const [country, setCountry] = useState<string>('Country')
-  const [number, setNumber] = useState<string>('+380995688412')
+  const [number, setNumber] = useState<string>('')
 
   const dispatch = useDispatch()
 

+ 3 - 3
src/components/Routes/PrivateRoute/PrivateRoute.tsx

@@ -2,13 +2,13 @@ import { Route, Redirect } from 'react-router-dom';
 import { useSelector } from 'react-redux';
 
 import { IPrivateProps} from '../../../typescript/components/Routes/interfaces';
-import { getName } from '../../../redux/authorization/selector'
+import { getToken } from '../../../redux/authorization/selector'
 
 function PrivateRoute({ children, ...routeProps }: IPrivateProps) {
-  const name = useSelector(getName)
+  const token = useSelector(getToken)
   return (
     <Route {...routeProps}>
-      {name ? children : <Redirect to="/z/" />}
+      {token ? children : <Redirect to="/z/" />}
     </Route>
   );
 }

+ 3 - 3
src/components/Routes/PublicRoute/PublicRoute.tsx

@@ -2,15 +2,15 @@ import { Route, Redirect } from 'react-router-dom';
 import { useSelector } from 'react-redux';
 
 import { IPublicProps } from '../../../typescript/components/Routes/interfaces';
-import { getName } from '../../../redux/authorization/selector'
+import { getToken } from '../../../redux/authorization/selector'
 
 function PublicRoute({
   children,
   restricted = false,
   ...routeProps
 }: IPublicProps) {
-  const name = useSelector(getName)
-  const shouldRedirect = name && restricted;
+  const token = useSelector(getToken)
+  const shouldRedirect = token && restricted;
 
   return (
     <Route {...routeProps}>

+ 7 - 5
src/redux/authorization/operations/index.ts

@@ -12,8 +12,7 @@ import { setToken, loginUser,updateCredentials,updateUserAvatar } from '../../..
 const asyncCreateUser = (name:string, lastName: string,avatar:object) => async (dispatch:any) => {
   try {
     dispatch(actionIsLoading(true));
-    const data = await updateCredentials(name, lastName);
-    console.log(data)
+    await updateCredentials(name, lastName);
   } catch (e) {
     console.log(encodeURI)
   } finally {
@@ -26,10 +25,13 @@ const asyncLogin = (number:string, code: string,cb:() => void ) => async (dispat
   try {
     dispatch(actionIsLoading(true));
     const data = await loginUser<{token:string,registration: boolean}>(number, code);
-    if(!data?.token) throw new Error('bad request')
-      dispatch(actionLogInSuccess(data.token))
+    if (!data?.token) throw new Error('bad request')
     setToken.set(data.token)
-    if(!data?.registration) cb()
+    if (data?.registration) {
+      cb()
+    } else {
+      dispatch(actionLogInSuccess(data.token))
+    }
   } catch (e) {
     dispatch(actionLogInReject());
     cb()