Skip to content

Commit 4a26a20

Browse files
Fix database store (#413)
1 parent d754a27 commit 4a26a20

File tree

10 files changed

+95
-61
lines changed

10 files changed

+95
-61
lines changed

dashboard-example/.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
DASHBOARD_READ_TOKEN=ENTERWRITETOKEN
2-
DASHBOARD_READ_WRITE=ENTERREADTOKEN
1+
DASHBOARD_READ_TOKEN=e75c10ba-749d-4451-8fb0-4ad972ebf970
2+
DASHBOARD_READ_WRITE=c754d13b-a294-462e-b0ef-71d2ad307426

dashboard-fe/components/with-auth.tsx

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,59 @@ import { publicConfig } from "../src/config";
33
import { useUser } from "@auth0/nextjs-auth0";
44
import { UserProvider } from "@auth0/nextjs-auth0";
55
import { withPageAuthRequired } from "@auth0/nextjs-auth0";
6-
6+
import { ApolloProvider } from "@apollo/client";
7+
// import store from "../src/store";
8+
const store = {};
79
export default function withAuth(InnerComponent: FC) {
810
if (!publicConfig.WITH_AUTH) {
911
const NoAuth = (props: object) => {
12+
console.log("NI auth");
1013
return <InnerComponent {...props} user={null} />;
1114
};
15+
NoAuth.getInitialProps = InnerComponent.getInitialProps;
16+
1217
return NoAuth;
1318
}
1419

1520
const Authenticated = withPageAuthRequired((props) => {
1621
const { user, error, isLoading } = useUser();
22+
const store = require("../src/store");
23+
console.log(store);
24+
console.log("ín auth wrapper");
1725
if (isLoading) return <div>Loading...</div>;
1826
if (error) return <div>{error.message}</div>;
19-
return <InnerComponent {...props} user={user} />;
27+
return (
28+
<ApolloProvider client={store.client}>
29+
<InnerComponent {...props} user={user} />
30+
</ApolloProvider>
31+
);
2032
});
2133

22-
const WrappedComponent = (props) => (
23-
<UserProvider>
24-
<Authenticated {...props} />
25-
</UserProvider>
26-
);
34+
const AuthRoute = (props) => {
35+
const { user, error, isLoading } = useUser();
36+
if (isLoading) return <div>Loading...</div>;
37+
if (error) return <div>{error.message}</div>;
38+
if (!user && typeof window !== "undefined") {
39+
window.location.href = "/api/auth/login";
40+
return null;
41+
}
42+
const store = require("../src/store").default;
43+
console.log(store);
44+
return (
45+
<ApolloProvider client={store.client}>
46+
<InnerComponent {...props} user={user} />
47+
</ApolloProvider>
48+
);
49+
};
50+
51+
const WrappedComponent = (props) => {
52+
return (
53+
<UserProvider>
54+
<AuthRoute {...props} />
55+
</UserProvider>
56+
);
57+
};
58+
WrappedComponent.getInitialProps = InnerComponent.getInitialProps;
2759

2860
return WrappedComponent;
2961
}

dashboard-fe/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@apollo/client": "^3.5.0",
3434
"@apollo/react-hooks": "^4.0.0",
3535
"@auth0/auth0-react": "1.0.0",
36-
"@auth0/nextjs-auth0": "1.5.0",
36+
"@auth0/nextjs-auth0": "^1.7.0",
3737
"@emotion/core": "10.0.27",
3838
"@emotion/react": "11.4.0",
3939
"@emotion/styled": "10.0.27",

dashboard-fe/pages/_app.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { ApolloProvider } from "@apollo/client";
44
import Head from "next/head";
55
import Script from "next/script";
66
import CssBaseline from "@material-ui/core/CssBaseline";
7-
import store from "../src/store";
7+
88
import withAuth from "../components/with-auth";
99

1010
function MyApp(props) {
1111
const { Component, pageProps } = props;
12+
console.log(pageProps);
1213
React.useEffect(() => {
1314
// Remove the server-side injected CSS.
1415
const jssStyles = document.querySelector("#jss-server-side");
@@ -18,7 +19,7 @@ function MyApp(props) {
1819
}, []);
1920

2021
return (
21-
<ApolloProvider client={store.client}>
22+
<>
2223
<Head>
2324
<meta
2425
name="viewport"
@@ -39,10 +40,11 @@ function MyApp(props) {
3940
gtag('config', 'G-EY5KSYXPTL');`,
4041
}}
4142
/>
42-
</ApolloProvider>
43+
</>
4344
);
4445
}
46+
4547
MyApp.getInitialProps = async (props) => {
46-
return props;
48+
return {};
4749
};
4850
export default withAuth(MyApp);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
import auth0 from "../../../src/auth0";
2-
export default auth0.handleAuth();
1+
import { handleAuth } from "@auth0/nextjs-auth0";
2+
console.log(process.env.AUTH0_CLIENT_SECRET);
3+
export default handleAuth();

dashboard-fe/pages/api/graphql.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import ModuleManager from "../../src/managers/Module";
77
import VersionManager from "../../src/managers/Version";
88
import { privateConfig } from "../../src/config";
99
import auth0 from "../../src/auth0";
10+
import { withApiAuthRequired, getSession } from "@auth0/nextjs-auth0";
11+
1012
import "../../src/webhooks";
1113
import { application } from "express";
1214
import { MongoClient } from "mongodb";
@@ -537,7 +539,7 @@ const apolloServer = new ApolloServer({
537539
if (res.hasValidToken) {
538540
user = { user: { email: res.hasValidToken } };
539541
} else {
540-
user = auth0.getSession(req, res);
542+
user = getSession(req, res);
541543
}
542544
if (!user) {
543545
res.status(401).json({
@@ -557,12 +559,7 @@ const apolloServer = new ApolloServer({
557559
}
558560
},
559561
});
560-
561-
const apolloHandler = apolloServer.start().then(() =>
562-
apolloServer.createHandler({
563-
path: "/api/graphql",
564-
})
565-
);
562+
const startedApolloServer = apolloServer.start();
566563

567564
async function runMiddleware(req: any, res: any, fn: any) {
568565
const callback = await fn;
@@ -592,6 +589,7 @@ const allowCors = async (req: any, res: any, next: any) => {
592589
);
593590

594591
if (req.method === "OPTIONS") {
592+
console.log("options call");
595593
res.status(200).end();
596594
return;
597595
}
@@ -636,15 +634,15 @@ async function handler(req: any, res: any) {
636634
const ts = Math.random();
637635
console.time("checkForTokens-" + ts);
638636

639-
await runMiddleware(req, res, allowCors);
637+
// await runMiddleware(req, res, allowCors);
640638

641639
// @ts-expect-error ts-migrate(2554) FIXME: Expected 1 arguments, but got 0.
642640
// let session: { noAuth: boolean; user: {} } = false;
643641
// if (process.env.WITH_AUTH && !req.query.token) {
644642
// session = auth0.getSession(req, res);
645643
// }
646644
let user = await checkForTokens(req.query.token || "noToken");
647-
645+
console.log(user);
648646
// if (
649647
// !tokens ||
650648
// req?.headers?.Authorization?.find((token) => tokens.includes(token))
@@ -683,7 +681,13 @@ async function handler(req: any, res: any) {
683681
// }
684682
// }
685683
//
686-
await runMiddleware(req, res, apolloHandler);
684+
await startedApolloServer;
685+
686+
const apolloHandler = apolloServer.createHandler({
687+
path: "/api/graphql",
688+
});
689+
690+
return apolloHandler(req, res);
687691
}
688692

689693
export const config = {

dashboard-fe/pages/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import Link from "next/link";
77
import { observer } from "mobx-react";
88
import { get } from "lodash";
99
import dynamic from "next/dynamic";
10-
import ApplicationsTable from "../components/ApplicationsTable";
11-
import Layout from "../components/Layout";
12-
import store from "../src/store";
1310

11+
const Layout = dynamic(() => import("../components/Layout"), { ssr: false });
12+
13+
const ApplicationsTable = dynamic(
14+
() => import("../components/ApplicationsTable"),
15+
{ ssr: false }
16+
);
1417
const ModuleChordChart = dynamic(
1518
() => import("../components/ModuleChordChart"),
1619
{ ssr: false }
@@ -63,6 +66,7 @@ const useHomeStyles = makeStyles({
6366
});
6467

6568
const Home = () => {
69+
const store = require("../src/store").default;
6670
const { data } = useQuery(GET_APPS, {
6771
variables: {
6872
environment: store.environment,

dashboard-fe/pages/protected-page.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

dashboard-fe/src/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const fetchUser = async () => {
88
return userState;
99
}
1010

11-
const res = await fetch("/api/me");
11+
const res = await fetch("/api/auth/me");
1212
userState = res.ok ? await res.json() : null;
1313
return userState;
1414
};

yarn.lock

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,21 @@
139139
promise-polyfill "^8.2.1"
140140
unfetch "^4.2.0"
141141

142-
"@auth0/nextjs-auth0@1.5.0":
143-
version "1.5.0"
144-
resolved "https://registry.yarnpkg.com/@auth0/nextjs-auth0/-/nextjs-auth0-1.5.0.tgz#d55cb34545ee2ddc394acce796edc368a2871206"
145-
integrity sha512-zfFTR9x8qxF0DFwUOTwQEDtj0mAjZQ5xNLBzRM5BG0qYhvsLC74MYZ5F0BM2cazVCqqLRydbrnTgbOEJ6wCIzg==
142+
"@auth0/nextjs-auth0@^1.7.0":
143+
version "1.7.0"
144+
resolved "https://registry.yarnpkg.com/@auth0/nextjs-auth0/-/nextjs-auth0-1.7.0.tgz#61af4e9315cf8e8b42ffcb2dad09ff92b7ce7cfa"
145+
integrity sha512-twz4f1A94i2ShJ6jB3UsFuVhzE0vJ1M/Ukq5bNBm/gpvCK6E/P8Ew2wQloak73+mXcV7eg5S+HOlmiKpcSe6mw==
146146
dependencies:
147147
base64url "^3.0.1"
148148
cookie "^0.4.1"
149-
debug "^4.3.2"
150-
futoin-hkdf "^1.3.2"
151-
http-errors "^1.8.0"
152-
joi "^17.4.0"
149+
debug "^4.3.3"
150+
futoin-hkdf "^1.4.2"
151+
http-errors "^1.8.1"
152+
joi "^17.5.0"
153153
jose "^2.0.5"
154154
on-headers "^1.0.2"
155-
openid-client "^4.7.4"
156-
tslib "^2.3.0"
155+
openid-client "^4.9.1"
156+
tslib "^2.3.1"
157157
url-join "^4.0.1"
158158

159159
@@ -6077,6 +6077,13 @@ debug@^3.1.0, debug@^3.1.1, debug@^3.2.6, debug@^3.2.7:
60776077
dependencies:
60786078
ms "^2.1.1"
60796079

6080+
debug@^4.3.3:
6081+
version "4.3.4"
6082+
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
6083+
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
6084+
dependencies:
6085+
ms "2.1.2"
6086+
60806087
debuglog@^1.0.1:
60816088
version "1.0.1"
60826089
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
@@ -7776,7 +7783,7 @@ functional-red-black-tree@^1.0.1:
77767783
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
77777784
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=
77787785

7779-
futoin-hkdf@^1.3.2:
7786+
futoin-hkdf@^1.4.2:
77807787
version "1.5.0"
77817788
resolved "https://registry.yarnpkg.com/futoin-hkdf/-/futoin-hkdf-1.5.0.tgz#f10cc4d32f1e26568ded58d5a6535a97aa3a064c"
77827789
integrity sha512-4CerDhtTgx4i5PKccQIpEp4T9wqmosPIP9Kep35SdCpYkQeriD3zddUVhrO1Fc4QvGhsAnd2rXyoOr5047mJEg==
@@ -8447,7 +8454,7 @@ [email protected]:
84478454
statuses ">= 1.5.0 < 2"
84488455
toidentifier "1.0.0"
84498456

8450-
[email protected], http-errors@^1.7.3, http-errors@^1.8.0:
8457+
[email protected], http-errors@^1.7.3, http-errors@^1.8.1:
84518458
version "1.8.1"
84528459
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.8.1.tgz#7c3f28577cbc8a207388455dbd62295ed07bd68c"
84538460
integrity sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==
@@ -9706,7 +9713,7 @@ [email protected]:
97069713
import-local "^3.0.2"
97079714
jest-cli "^26.6.3"
97089715

9709-
joi@^17.4.0:
9716+
joi@^17.5.0:
97109717
version "17.6.0"
97119718
resolved "https://registry.yarnpkg.com/joi/-/joi-17.6.0.tgz#0bb54f2f006c09a96e75ce687957bd04290054b2"
97129719
integrity sha512-OX5dG6DTbcr/kbMFj0KGYxuew69HPcAE3K/sZpEV2nP6e/j/C0HV+HNiBPCASxdx5T7DMoa0s8UeHWMnb6n2zw==
@@ -11542,7 +11549,7 @@ opener@^1.5.2:
1154211549
resolved "https://registry.yarnpkg.com/opener/-/opener-1.5.2.tgz#5d37e1f35077b9dcac4301372271afdeb2a13598"
1154311550
integrity sha512-ur5UIdyw5Y7yEj9wLzhqXiy6GZ3Mwx0yGI+5sMn2r0N0v3cKJvUmFH5yPP+WXh9e0xfyzyJX95D8l088DNFj7A==
1154411551

11545-
openid-client@^4.7.4:
11552+
openid-client@^4.9.1:
1154611553
version "4.9.1"
1154711554
resolved "https://registry.yarnpkg.com/openid-client/-/openid-client-4.9.1.tgz#4f00a9d1566c0fa08f0dd5986cf0e6b1e5d14186"
1154811555
integrity sha512-DYUF07AHjI3QDKqKbn2F7RqozT4hyi4JvmpodLrq0HHoNP7t/AjeG/uqiBK1/N2PZSAQEThVjDLHSmJN4iqu/w==
@@ -14771,7 +14778,7 @@ tslib@^1.10.0, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3:
1477114778
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
1477214779
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==
1477314780

14774-
tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0:
14781+
tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.3.1:
1477514782
version "2.3.1"
1477614783
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
1477714784
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==

0 commit comments

Comments
 (0)