Skip to content

Commit 279fc12

Browse files
committed
Working with login and initial folders front
1 parent 3111969 commit 279fc12

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import { Route, Routes } from "react-router-dom"
2+
import { Home } from "../Pages/Home"
13

24
export const VoteRoutes = () => {
35
return (
4-
<div>VoteRoutes</div>
6+
<div>
7+
<Routes>
8+
<Route path='/*' element={<Home />} />
9+
10+
</Routes>
11+
</div>
512
)
613
}

votingWebSockets-Front/src/auth/pages/LoginPage.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { useDispatch, useSelector } from "react-redux";
22
import { useForm } from "../../hooks/useForm";
33
import { AppDispatch, RootState } from "../../redux/store";
44
import { login } from "../../redux/Reducers/auth/authSlice";
5+
import { useNavigate } from "react-router-dom";
56
export const LoginPage = () => {
7+
const navigate = useNavigate();
68

79
const { formState, onInputChange, onResetForm } = useForm({
810
email: '',
@@ -16,7 +18,9 @@ export const LoginPage = () => {
1618
dispatch(login({ email: formState.email, password: formState.password }))
1719
.unwrap()
1820
.then(() => {
19-
console.log("Login successful");
21+
navigate("/", {
22+
replace: true,
23+
});
2024
onResetForm();
2125
})
2226
.catch((err) => {

votingWebSockets-Front/src/redux/Reducers/auth/authSlice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface AuthState {
1414

1515

1616
const initialState: AuthState = {
17-
isAuthenticated: false,
17+
isAuthenticated: !!localStorage.getItem('authToken'),
1818
role: '',
1919
loading: false,
2020
error: null,
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
import { useSelector } from "react-redux";
12
import { Navigate } from "react-router-dom";
3+
import { RootState } from "../redux/store";
24

35

46
interface PrivateRouterProps {
57
children: React.ReactNode;
68
}
79

810
export const PrivateRouter: React.FC<PrivateRouterProps> = ({ children }) => {
9-
const logged: boolean = false;
11+
const isAuthenticated = useSelector((state: RootState) => state.auth.isAuthenticated);
1012

11-
return (logged)
12-
? children
13-
: <Navigate to="/login" />
13+
if (!isAuthenticated) {
14+
return <Navigate to="/login" />;
15+
}
16+
17+
return <>{children}</>;
1418
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import { useSelector } from "react-redux";
12
import { Navigate } from "react-router-dom";
3+
import { RootState } from "../redux/store";
24
interface PublicRouterProps {
35
children: React.ReactNode;
46
}
57

68
export const PublicRouter: React.FC<PublicRouterProps> = ({ children }) => {
79

8-
const logged: boolean = false;
9-
return (!logged)
10-
? children
11-
: <Navigate to="/home" />
10+
const isAuthenticated = useSelector((state: RootState) => state.auth.isAuthenticated);
11+
if (isAuthenticated) {
12+
return <Navigate to="/home" replace />;
13+
}
14+
15+
return <>{children}</>;
1216
}

0 commit comments

Comments
 (0)