Skip to content
Snippets Groups Projects
Commit 1670c954 authored by robban64's avatar robban64
Browse files

Merge branch 'dev' of...

Merge branch 'dev' of https://gitlab.liu.se/tddd96-grupp11/teknikattan-scoring-system into 47-implement-marshmallow
parents 944205d4 b6c1cd67
No related branches found
No related tags found
1 merge request!43Resolve "Implement marshmallow"
Pipeline #38812 passed
......@@ -11,11 +11,15 @@ interface SecureRouteProps extends RouteProps {
/** Utility component to use for authentication, replace all routes that should be private with secure routes*/
const SecureRoute: React.FC<SecureRouteProps> = ({ login, component: Component, ...rest }: SecureRouteProps) => {
const authenticated = useAppSelector((state) => state.user.authenticated)
const loading = useAppSelector((state) => state.user.loading)
const [initialized, setInitialized] = React.useState(false)
useEffect(() => {
CheckAuthentication()
const waitForAuthentication = async () => {
await CheckAuthentication()
setInitialized(true)
}
waitForAuthentication()
}, [])
if (!loading) {
if (initialized) {
if (login)
return (
<Route {...rest} render={(props) => (authenticated ? <Redirect to="/admin" /> : <Component {...props} />)} />
......
......@@ -8,7 +8,7 @@ const UnAuthorized = () => {
logoutUser()(store.dispatch)
}
export const CheckAuthentication = () => {
export const CheckAuthentication = async () => {
const authToken = localStorage.token
if (authToken) {
const decodedToken: any = jwtDecode(authToken)
......@@ -16,7 +16,7 @@ export const CheckAuthentication = () => {
axios.defaults.headers.common['Authorization'] = authToken
store.dispatch({ type: Types.LOADING_USER })
console.log('loading user')
axios
await axios
.get('/users')
.then((res) => {
store.dispatch({ type: Types.SET_AUTHENTICATED })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment