forked from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Uo277382' of https://github.com/Arquisoft/wiq_es3a into…
… Uo277382
- Loading branch information
Showing
7 changed files
with
149 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Navigate, Outlet } from "react-router-dom"; | ||
import { useAuth } from "./login/AuthProvider"; | ||
import Navbar from "./Navbar"; | ||
|
||
export const ProtectedRoute = () => { | ||
const { token } = useAuth(); | ||
|
||
// Check if the user is authenticated | ||
if (!token) { | ||
// If not authenticated, redirect to the login page | ||
return <Navigate to="/login" />; | ||
} | ||
|
||
// If authenticated, render the child routes | ||
return <div><Navbar></Navbar><Outlet /></div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { RouterProvider, createBrowserRouter } from "react-router-dom"; | ||
import { useAuth } from "./login/AuthProvider"; | ||
import { ProtectedRoute } from "./ProtectedRoute"; | ||
import Home from "./Home"; | ||
import Inicio from './Inicio'; | ||
import Primera from './Primera'; | ||
import Game from './QuizGame'; | ||
import Navbar from "./Navbar"; | ||
|
||
const Routes = () => { | ||
const { token } = useAuth(); | ||
|
||
// Define public routes accessible to all users | ||
const routesForPublic = [ | ||
{ | ||
path: "/", | ||
element: <Primera />, | ||
}, | ||
{ | ||
path: "/login", | ||
element: <Inicio />, | ||
}, | ||
]; | ||
|
||
// Define routes accessible only to authenticated users | ||
const routesForAuthenticatedOnly = [ | ||
{ | ||
path: "/", | ||
element: <ProtectedRoute />, // Wrap the component in ProtectedRoute | ||
children: [ | ||
{ | ||
path: "/", | ||
element: <Primera />, | ||
}, | ||
{ | ||
path: "/game", | ||
element: <Game />, | ||
}, | ||
{ | ||
path: "/home", | ||
element: <Home />, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
// Define routes accessible only to non-authenticated users | ||
const routesForNotAuthenticatedOnly = [ | ||
|
||
]; | ||
|
||
// Combine and conditionally include routes based on authentication status | ||
const router = createBrowserRouter([ | ||
...routesForPublic, | ||
...(!token ? routesForNotAuthenticatedOnly : []), | ||
...routesForAuthenticatedOnly, | ||
]); | ||
|
||
// Provide the router configuration using RouterProvider | ||
return <RouterProvider router={router}> </RouterProvider> | ||
; | ||
}; | ||
|
||
export default Routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import axios from "axios"; | ||
import { createContext, useContext, useEffect, useMemo, useState } from "react"; | ||
|
||
const AuthContext = createContext(); | ||
|
||
const AuthProvider = ({ children }) => { | ||
// State to hold the authentication token | ||
const [token, setToken_] = useState(localStorage.getItem("token")); | ||
|
||
// Function to set the authentication token | ||
const setToken = (newToken) => { | ||
setToken_(newToken); | ||
}; | ||
|
||
useEffect(() => { | ||
if (token) { | ||
axios.defaults.headers.common["Authorization"] = "Bearer " + token; | ||
localStorage.setItem('token',token); | ||
} else { | ||
delete axios.defaults.headers.common["Authorization"]; | ||
localStorage.removeItem('token') | ||
} | ||
}, [token]); | ||
|
||
// Memoized value of the authentication context | ||
const contextValue = useMemo( | ||
() => ({ | ||
token, | ||
setToken, | ||
}), | ||
[token] | ||
); | ||
|
||
// Provide the authentication context to the children components | ||
return ( | ||
<AuthContext.Provider value={contextValue}>{children}</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuth = () => { | ||
return useContext(AuthContext); | ||
}; | ||
|
||
export default AuthProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters