-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from SCCapstone/dev
Merge dev
- Loading branch information
Showing
8 changed files
with
160 additions
and
8 deletions.
There are no files selected for viewing
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
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,14 @@ | ||
import React, { useContext } from 'react'; | ||
import { Navigate, Outlet } from "react-router-dom"; | ||
import UserContext from '../context/userContext'; | ||
|
||
export const ProtectedRoute = ( { children} ) => { | ||
const { user } = useContext(UserContext); | ||
|
||
console.log('user') | ||
console.log(user); | ||
return ( | ||
|
||
user ? children : <Navigate to='/signin'/> | ||
) | ||
} |
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,10 @@ | ||
import { createContext } from 'react'; | ||
|
||
const UserContext = createContext({ | ||
user: null, | ||
token: null, | ||
login: () => {}, | ||
logout: () => {}, | ||
}); | ||
|
||
export default UserContext; |
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,61 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import UserContext from './userContext'; | ||
import UserService from '../services/userService'; | ||
import { startConnection, stopConnection } from '../services/signalrService'; | ||
|
||
|
||
const UserProvider = ({ children }) => { | ||
const [user, setUser] = useState(null); | ||
const [token, setToken] = useState(localStorage.getItem('token') || null); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
const fetchCurrentUser = async () => { | ||
try { | ||
if (token) { | ||
const currentUser = await UserService.getUserprofile('current'); | ||
console.log(currentUser); | ||
setUser(currentUser); | ||
} else { | ||
setUser(null); | ||
} | ||
} catch (error) { | ||
console.error("Error fetching current user:", error.message); | ||
setUser(null); | ||
} | ||
}; | ||
|
||
fetchCurrentUser(); | ||
}, [token]); | ||
|
||
const login = (newToken) => { | ||
console.log('token'); | ||
console.log(newToken); | ||
setToken(newToken); | ||
localStorage.setItem('token', newToken); | ||
startConnection(); | ||
}; | ||
|
||
const logout = () => { | ||
setToken(null); | ||
localStorage.removeItem('token'); | ||
stopConnection(); | ||
}; | ||
|
||
// useEffect(() => { | ||
// // Redirect to home page if the user is not null | ||
// if (user) { | ||
// navigate('/home'); | ||
// } | ||
// }, [user, navigate]); | ||
|
||
return ( | ||
<UserContext.Provider value={{ user, token, login, logout }}> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
}; | ||
|
||
export default UserProvider; | ||
|
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