-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPrivateRoute.js
More file actions
42 lines (36 loc) · 1.24 KB
/
PrivateRoute.js
File metadata and controls
42 lines (36 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import React from "react"
import { connect } from 'react-redux'
import { navigate } from "gatsby"
import { isAuthorizedUser } from '../utils/authorizedGroups';
import { doLogin } from 'openstack-uicore-foundation/lib/security/methods'
import HeroComponent from "../components/HeroComponent";
const PrivateRoute = ({ children, location, isLoggedUser, user, isIdTokenAlive, ...rest}) => {
if (!isLoggedUser) {
if(typeof window !== 'undefined') {
// do login after page is loaded and Google Tag Manager is loaded
function checkIfAnalyticsLoaded() {
if (typeof window.ga === 'function' && Array.isArray(window.dataLayer)) {
doLogin(`${location.pathname}`);
} else {
setTimeout(checkIfAnalyticsLoaded, 500);
}
}
checkIfAnalyticsLoaded();
}
return <HeroComponent title={'Checking Credentials ...'}/>
}
if (!isAuthorizedUser(user)) {
navigate('/', {
state: {
error: 'no-authz'
}
})
return <HeroComponent title={'User not Authorized ...'}/>
}
return children;
}
const mapStateToProps = ({ loggedUserState }) => ({
isLoggedUser: loggedUserState.isLoggedUser,
user: loggedUserState.member
})
export default connect(mapStateToProps)(PrivateRoute)