AOA,
I want to return the output of useAuth
.the output will be returned to ProtectedRoute
component and as it is will be returned to layout.js
. let me know if you need more info
Private Route.js
JavaScript
x
68
68
1
import React from 'react';
2
import { Navigate, Outlet } from 'react-router-dom'
3
import axios from 'axios';
4
import Cookies from 'universal-cookie';
5
import { useEffect } from 'react';
6
let ast=null;
7
8
const ProtectedRoute = ({ component: Component, rest }) => {
9
10
11
12
useEffect(() => {
13
async function useAuth () {
14
let as=null;
15
const cookies = new Cookies();
16
//const user = localStorage.getItem("authToken")
17
//const user = cookies.get("authToken");
18
const _id="633e9ed97968510b2689851b";
19
20
21
if (user) {
22
23
const config = {
24
25
headers: {
26
"Content-Type": "application/json",
27
},
28
};
29
try{
30
31
const {data}=await axios.post(
32
`http://localhost:5000/api/auth/Veri`,
33
{
34
_id
35
},
36
config
37
);
38
39
console.log("data" +data.email);
40
if(data.email){
41
42
return (<Outlet />)
43
}
44
else{
45
console.log(<Outlet />);
46
return <Navigate to="authentication/card/login" />;
47
}
48
}
49
catch(error){
50
console.log("Error ");
51
ast=false;
52
return <Navigate to="authentication/card/login" />;
53
}
54
55
}
56
57
else {
58
ast=false;
59
return <Navigate to="authentication/card/login" />
60
}
61
}
62
63
64
65
useAuth() ;
66
67
}, []);
68
layout.js
JavaScript
1
11
11
1
<Route element={<PrivateRoutes/>}>
2
<Route element={<MainLayout />}>
3
{/*Dashboard*/}
4
<Route path="/" element={<Dashboard />} />
5
<Route path="dashboard/analytics" element={<Analytics />} />
6
<Route path="dashboard/crm" element={<Crm />} />
7
<Route path="dashboard/saas" element={<Saas />} />
8
<Route path="dashboard/e-commerce" element={<Ecommerce />} />
9
</Route>
10
</Route>
11
Advertisement
Answer
You can not use a component returned from useEffect instead you should be using a state for this.
JavaScript
1
65
65
1
import React from 'react';
2
import { Navigate, Outlet } from 'react-router-dom'
3
import axios from 'axios';
4
import Cookies from 'universal-cookie';
5
import { useEffect, useState, useNavigate } from 'react';
6
let ast=null;
7
8
const ProtectedRoute = ({ component: Component, rest }) => {
9
10
const [authenticated, setAuthenticated] = useState(null);
11
const navigate = useNavigate();
12
13
useEffect(() => {
14
let as=null;
15
const cookies = new Cookies();
16
const _id="633e9ed97968510b2689851b";
17
18
//IDK where the **user** comes from
19
if (user) {
20
const config = {
21
headers: {
22
"Content-Type": "application/json",
23
},
24
};
25
try{
26
const {data}=await axios.post(
27
`http://localhost:5000/api/auth/Veri`,
28
{
29
_id
30
},
31
config
32
);
33
console.log("data" ,data.email);
34
if(data.email){
35
setAuthenticated(true);
36
}
37
else{
38
setAuthenticated(false);
39
}
40
}
41
catch(error){
42
console.log("Error ");
43
ast=false;
44
setAuthenticated(false);
45
}
46
}
47
else {
48
ast=false;
49
setAuthenticated(false);
50
}
51
}
52
, []);
53
54
useEffect(()=>{
55
if(authenticated==false){
56
navigate("authentication/card/login");
57
}
58
}, []);
59
60
return (
61
authenticated==true ? <Outlet/> : null
62
)
63
// You can replace the null with some loader.
64
}
65