Main project structure #3
@ -27,7 +27,6 @@ const PrivateRoute = () => {
|
|||||||
const [checked, setChecked] = useState(false);
|
const [checked, setChecked] = useState(false);
|
||||||
const [isValid, setIsValid] = useState(false);
|
const [isValid, setIsValid] = useState(false);
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const checkAuth = async () => {
|
const checkAuth = async () => {
|
||||||
if (!token) {
|
if (!token) {
|
||||||
@ -85,7 +84,7 @@ function App() {
|
|||||||
<Route path='/settings' element={<Settings />} />
|
<Route path='/settings' element={<Settings />} />
|
||||||
</Route>
|
</Route>
|
||||||
|
|
||||||
<Route path="*" element={<div>404</div>} />
|
<Route path="*" element={<h1 className="not-found">404<br/>Not Found</h1>} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</Router>
|
</Router>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@ -31,3 +31,10 @@ body {
|
|||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
max-height: 100%;
|
max-height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.not-found {
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translate(-50%, -50%);
|
||||||
|
}
|
||||||
|
|||||||
@ -13,14 +13,15 @@ const Login = () => {
|
|||||||
const { setToken } = useAuth();
|
const { setToken } = useAuth();
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
const searchPath = new URLSearchParams(location.search);
|
||||||
|
const redirectPath = searchPath.get('to') || "/";
|
||||||
|
|
||||||
const [usernameEmpty, setUsernameEmpty] = useState(false);
|
const [usernameEmpty, setUsernameEmpty] = useState(false);
|
||||||
const [passwordEmpty, setPasswordEmpty] = useState(false);
|
const [passwordEmpty, setPasswordEmpty] = useState(false);
|
||||||
|
|
||||||
const from = location.state?.from?.pathname || "/";
|
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
@ -35,7 +36,7 @@ const Login = () => {
|
|||||||
if ((username === "admin" && password === "1234") || (username === "a" && password === "a")) {
|
if ((username === "admin" && password === "1234") || (username === "a" && password === "a")) {
|
||||||
const token = "todo.jwt.token";
|
const token = "todo.jwt.token";
|
||||||
setToken(token);
|
setToken(token);
|
||||||
navigate(from, { replace: true });
|
navigate(redirectPath, { replace: true });
|
||||||
} else {
|
} else {
|
||||||
toast.error("Wrong login or password");
|
toast.error("Wrong login or password");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user