BOD-2 More cleanup after fixing JWT tokens.

This commit is contained in:
Patrick Fic
2020-03-24 18:59:56 -07:00
parent 1c4a5d5b63
commit 1409ef058c
11 changed files with 38 additions and 359 deletions

View File

@@ -6,26 +6,17 @@ import { split } from "apollo-link";
import { setContext } from "apollo-link-context";
import { HttpLink } from "apollo-link-http";
import apolloLogger from "apollo-link-logger";
import { RetryLink } from "apollo-link-retry";
import { WebSocketLink } from "apollo-link-ws";
import { getMainDefinition } from "apollo-utilities";
import React, { Component } from "react";
import GlobalLoadingBar from "../components/global-loading-bar/global-loading-bar.component";
import SpinnerComponent from "../components/loading-spinner/loading-spinner.component";
import { auth } from "../firebase/firebase.utils";
import errorLink from "../graphql/apollo-error-handling";
import App from "./App";
import { RetryLink } from "apollo-link-retry";
import { auth } from "../firebase/firebase.utils";
export default class AppContainer extends Component {
// constructor() {
// super();
// }
state = {
client: null,
loaded: false
};
async componentDidMount() {
constructor() {
super();
const httpLink = new HttpLink({
uri: process.env.REACT_APP_GRAPHQL_ENDPOINT
});
@@ -34,36 +25,25 @@ export default class AppContainer extends Component {
uri: process.env.REACT_APP_GRAPHQL_ENDPOINT_WS,
options: {
//lazy: true,
reconnect: true,
connectionParams: () => {
const token = localStorage.getItem("token");
if (token) {
return {
headers: {
authorization: token ? `Bearer ${token}` : ""
}
};
}
}
reconnect: true
// connectionParams: () => {
// const token = localStorage.getItem("token");
// if (token) {
// return {
// headers: {
// authorization: token ? `Bearer ${token}` : ""
// }
// };
// }
// }
}
});
// const wsLink = new WebSocketLink({
// uri: websocketUrl,
// options: {
// reconnect: true
// },
// webSocketImpl: ws
// });
const subscriptionMiddleware = {
applyMiddleware: async (options, next) => {
options.authToken = await auth.currentUser.getIdToken(true);
next();
}
};
// add the middleware to the web socket link via the Subscription Transport client
wsLink.subscriptionClient.use([subscriptionMiddleware]);
const link = split(
@@ -87,28 +67,7 @@ export default class AppContainer extends Component {
httpLink
);
// const authLink = setContext((_, { headers }) => {
// const token = localStorage.getItem("token");
// console.log("In the auth link");
// //Check to see if the token has expired. If it has, then
// if (token) {
// return {
// headers: {
// ...headers,
// authorization: token ? `Bearer ${token}` : ""
// }
// };
// } else {
// return { headers };
// }
// });
const authLink = setContext((_, { headers }) => {
// return AsyncTokenLookup()
// .then((userToken) => {
// token = userToken;
// return { token };
// })
return auth.currentUser.getIdToken().then(token => {
if (token) {
return {
@@ -123,35 +82,6 @@ export default class AppContainer extends Component {
});
});
// const authLink = new ApolloLink((operation, forward) => {
// console.log("The new auth link");
// const token = localStorage.getItem("token");
// auth.currentUser.
// auth.currentUser.getIdToken().then(t => {
// console.log("token", token);
// if (token) {
// operation.setContext({
// headers: {
// Authorization: `Bearer ${token}`
// }
// });
// operation.setContext({
// headers: {
// Authorization: `Bearer ${token}`
// }
// });
// return forward(operation);
// }
// });
// return forward(operation);
// });
const middlewares = [];
if (process.env.NODE_ENV === "development") {
middlewares.push(apolloLogger);
}
const retryLink = new RetryLink({
delay: {
initial: 300,
@@ -164,27 +94,25 @@ export default class AppContainer extends Component {
}
});
const middlewares = [];
if (process.env.NODE_ENV === "development") {
middlewares.push(apolloLogger);
}
middlewares.push(retryLink.concat(errorLink.concat(authLink.concat(link))));
const cache = new InMemoryCache();
const client = new ApolloClient({
link: ApolloLink.from(middlewares),
cache,
connectToDevTools: true
});
this.setState({
client,
loaded: true
});
this.state = { client };
}
render() {
const { client, loaded } = this.state;
if (!loaded) {
return <SpinnerComponent message='Connecting to Bodyshop.app Database' />;
}
const { client } = this.state;
return (
<ApolloProvider client={client}>

View File

@@ -1,27 +1 @@
@import "~antd/dist/antd.css";
/* .ant-layout-header {
position: absolute;
top: 0px;
left: 0px;
height: 5vh;
right: 0px;
overflow: hidden;
}
.ant-layout-content {
position: absolute;
top: 5vh;
bottom: 3vh;
left: 0px;
right: 0px;
overflow: auto;
}
.ant-layout-footer {
position: absolute;
bottom: 0px;
height: 3vh;
left: 0px;
right: 0px;
overflow: hidden;
} */
@import "~antd/dist/antd.css";

View File

@@ -38,7 +38,7 @@ export default connect(
const { t } = useTranslation();
if (currentUser && currentUser.language)
i18next.changeLanguage(currentUser.language, (err, t) => {
i18next.changeLanguage(currentUser.language, err => {
if (err)
return console.log("Error encountered when changing languages.", err);
});
@@ -51,7 +51,7 @@ export default connect(
<div>
<Switch>
<ErrorBoundary>
<Suspense fallback={<LoadingSpinner message='In Suspense.' />}>
<Suspense fallback={<LoadingSpinner message='App.Js Suspense' />}>
<Route exact path='/' component={LandingPage} />
<Route exact path='/unauthorized' component={Unauthorized} />