Skip to content

Commit

Permalink
feat (issue platzi#14): Protección de Rutas
Browse files Browse the repository at this point in the history
  • Loading branch information
Yotis56 committed Feb 17, 2024
1 parent a279795 commit 893b4bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/Components/ProtectedRoute/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useContext } from "react"
import { ShoppingCartContext } from "../../Context"
import { Navigate, Outlet } from "react-router-dom"

export const ProtectedRoute = () => {
const { isLogged } = useContext(ShoppingCartContext)

return (
isLogged ? <Outlet /> : <Navigate replace to='/sign-in' />
)
}
2 changes: 1 addition & 1 deletion src/Components/SignUp/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const SignUp = () => {
return (
<div className="signUp-container flex flex-col w-80">
<h2 className='font-bold mb-3 text-center text-lg'>Welcome</h2>
<form action="">
<form action="" id='signUp_form'>
<div className="input-container flex flex-col mb-4">
<label htmlFor="name" className='text-sm'>Your Name:</label>
<input type="text" id='name' name='name' placeholder='John Doe' value={newUser.name} onChange={handleInputChange} className='py-2 px-4 border border-slate-900 rounded-lg outline-none'/>
Expand Down
27 changes: 16 additions & 11 deletions src/Pages/App/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,32 @@ import SignIn from '../SignIn'
import Navbar from '../../Components/Navbar'
import CheckoutSideMenu from '../../Components/CheckoutSideMenu'
import './App.css'
import { ProtectedRoute } from '../../Components/ProtectedRoute'

const AppRoutes = () => {
let routes = useRoutes([
{ path: '/', element: <Home /> },
{ path: '/clothes', element: <Home /> },
{ path: '/electronics', element: <Home /> },
{ path: '/furnitures', element: <Home /> },
{ path: '/toys', element: <Home /> },
{ path: '/othes', element: <Home /> },
{ path: '/my-account', element: <MyAccount /> },
{ path: '/my-order', element: <MyOrder /> },
{ path: '/my-orders', element: <MyOrders /> },
{ path: '/my-orders/last', element: <MyOrder /> },
{ path: '/my-orders/:id', element: <MyOrder /> },
{element: <ProtectedRoute />, children: [
{ path: '/', element: <Home /> },
{ path: '/clothes', element: <Home /> },
{ path: '/electronics', element: <Home /> },
{ path: '/furnitures', element: <Home /> },
{ path: '/toys', element: <Home /> },
{ path: '/othes', element: <Home /> },
{ path: '/my-account', element: <MyAccount /> },
{ path: '/my-order', element: <MyOrder /> },
{ path: '/my-orders', element: <MyOrders /> },
{ path: '/my-orders/last', element: <MyOrder /> },
{ path: '/my-orders/:id', element: <MyOrder /> },
]},
{ path: '/sign-in', element: <SignIn /> },
{ path: '/*', element: <NotFound /> },
])

return routes
}



const App = () => {
return (
<ShoppingCartProvider>
Expand Down

0 comments on commit 893b4bf

Please sign in to comment.