This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
71 lines (61 loc) · 1.68 KB
/
App.tsx
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 from "react";
import "./App.css";
import awsconfig from "./aws-exports";
import { ApolloLink } from "apollo-link";
import { createSubscriptionHandshakeLink } from "aws-appsync-subscription-link";
import { createAuthLink } from "aws-appsync-auth-link";
import ApolloClient from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloProvider } from "react-apollo";
import EventList from "./EventList";
import LatestEvents from "./LatestEvents";
import Auth from "@aws-amplify/auth";
import useAmplifyAuth from "./useAmplifyAuth";
Auth.configure(awsconfig);
const getAccessToken = (): Promise<string> => {
return Auth.currentSession().then(session => {
return session.getAccessToken().getJwtToken();
});
};
const config = {
url: awsconfig.aws_appsync_graphqlEndpoint,
region: awsconfig.aws_appsync_region,
auth: {
type: awsconfig.aws_appsync_authenticationType,
jwtToken: getAccessToken
},
disableOffline: true
};
const link = ApolloLink.from([
// @ts-ignore
createAuthLink(config),
// @ts-ignore
createSubscriptionHandshakeLink(config)
]);
export const client = new ApolloClient({
link,
cache: new InMemoryCache({ addTypename: false })
});
const App = () => {
const {
state: { user },
handleSignout
} = useAmplifyAuth();
return !user ? (
<div>
<button onClick={() => Auth.federatedSignIn()}>Open Hosted UI</button>
</div>
) : (
<div className="App">
<button onClick={handleSignout}>Sign Out</button>
<EventList />
<LatestEvents />
</div>
);
};
const WithProvider = () => (
<ApolloProvider client={client}>
<App />
</ApolloProvider>
);
export default WithProvider;