-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrap-with-provider.js
71 lines (60 loc) · 1.92 KB
/
wrap-with-provider.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React, { Fragment } from "react";
import { navigate } from "gatsby";
import { useLocation } from "@reach/router";
import { withTheme } from "styled-components";
import { setLayoutBase } from "./src/api/Layout";
import { setFirebaseClass } from "./src/api/Firebase";
import {
setWithAuthorizationWrapper,
WithAuthorizationClass,
} from "./src/api/Session";
import Firebase from "./src/components/Firebase";
import GlobalStyle, { Centered } from "./src/styles/global";
import { pathPrefix } from "./gatsby-config";
import "bootstrap/dist/css/bootstrap.min.css";
import "react-bootstrap-range-slider/dist/react-bootstrap-range-slider.css";
const fn = ({ element }) => {
setFirebaseClass(Firebase);
const WithAuthorizationWrapper = (props) => {
const location = useLocation();
const savePathname = () =>
window.localStorage.setItem(
"pathname",
location.pathname.replace(pathPrefix, "")
);
const AuthorizationFailed = () => (
<Centered>
<h3>You don't have permission to view this page!</h3>
<p>If you believe you should have access, please contact an admin.</p>
</Centered>
);
return (
<WithAuthorizationClass
firebaseAuthNext={(authUser) => {
if (!authUser) {
savePathname();
navigate("/");
}
}}
firebaseAuthFallback={() => {
savePathname();
navigate("/");
}}
authorizationFailed={<AuthorizationFailed />}
{...props}
/>
);
};
// would've preferred to call this in gatsby-browser onClientEntry, but can't do Queries in there
setWithAuthorizationWrapper(WithAuthorizationWrapper);
const LayoutBase = withTheme(({ theme, children }) => (
<Fragment>
<GlobalStyle theme={theme} />
{children}
</Fragment>
));
LayoutBase.displayName = "LayoutBase";
setLayoutBase(LayoutBase);
return element;
};
export default fn;