Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit c94b6eb

Browse files
Integrated JWT Authorization
- Updated schema.graphql, react-auth0-spa, and App component
1 parent 4c9c42e commit c94b6eb

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

todo-app-react/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@testing-library/user-event": "^7.1.2",
1111
"apollo-cache-inmemory": "^1.6.6",
1212
"apollo-client": "^2.6.10",
13+
"apollo-link-context": "^1.0.20",
1314
"apollo-link-http": "^1.5.17",
1415
"classnames": "^2.2.6",
1516
"graphql-tag": "^2.10.3",

todo-app-react/schema.graphql

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
type Task {
1+
type Task @auth(
2+
query: { rule: """
3+
query($USER: String!) {
4+
queryTask {
5+
user(filter: { username: { eq: $USER } }) {
6+
__typename
7+
}
8+
}
9+
}"""}), {
210
id: ID!
311
title: String! @search(by: [fulltext])
412
completed: Boolean! @search
@@ -7,7 +15,9 @@ type Task {
715

816
type User {
917
username: String! @id @search(by: [hash])
10-
name: String
18+
name: String @search(by: [exact])
1119
tasks: [Task] @hasInverse(field: user)
1220
}
1321

22+
23+
# Authorization X-Auth-Token https://dgraph.io/jwt/claims RS256 "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp/qw/KXH23bpOuhXzsDp\ndo9bGNqjd/OkH2LkCT0PKFx5i/lmvFXdd04fhJD0Z0K3pUe7xHcRn1pIbZWlhwOR\n7siaCh9L729OQjnrxU/aPOKwsD19YmLWwTeVpE7vhDejhnRaJ7Pz8GImX/z/Xo50\nPFSYdX28Fb3kssfo+cMBz2+7h1prKeLZyDk30ItK9MMj9S5y+UKHDwfLV/ZHSd8m\nVVEYRXUNNzLsxD2XaEC5ym2gCjEP1QTgago0iw3Bm2rNAMBePgo4OMgYjH9wOOuS\nVnyvHhZdwiZAd1XtJSehORzpErgDuV2ym3mw1G9mrDXDzX9vr5l5CuBc3BjnvcFC\nFwIDAQAB\n-----END PUBLIC KEY-----"

todo-app-react/src/App.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { InMemoryCache } from "apollo-cache-inmemory";
66
import { ApolloProvider } from "@apollo/react-hooks";
77
import { createHttpLink } from "apollo-link-http";
88
import { useAuth0 } from "./react-auth0-spa";
9+
import { setContext } from "apollo-link-context";
910

1011
import TodoApp from './TodoApp';
1112
import NavBar from "./NavBar";
@@ -15,26 +16,36 @@ import PrivateRoute from "./PrivateRoute";
1516
import config from "./config.json";
1617
import './App.css';
1718

18-
const createApolloClient = () => {
19+
const createApolloClient = token => {
1920
const httpLink = createHttpLink({
2021
uri: config.graphqlUrl,
2122
options: {
2223
reconnect: true,
2324
},
2425
});
2526

27+
const authLink = setContext((_, { headers }) => {
28+
// return the headers to the context so httpLink can read them
29+
return {
30+
headers: {
31+
...headers,
32+
"X-Auth-Token": token,
33+
},
34+
};
35+
});
36+
2637
return new ApolloClient({
27-
link: httpLink,
38+
link: authLink.concat(httpLink),
2839
cache: new InMemoryCache()
2940
});
3041
}
3142

32-
const App = () => {
43+
const App = ({idToken}) => {
3344
const { loading } = useAuth0();
3445
if (loading) {
3546
return <div>Loading...</div>;
3647
}
37-
const client = createApolloClient();
48+
const client = createApolloClient(idToken);
3849
return (
3950
<ApolloProvider client={client}>
4051
<div>

todo-app-react/src/react-auth0-spa.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const Auth0Provider = ({
1717
const [auth0Client, setAuth0] = useState();
1818
const [loading, setLoading] = useState(true);
1919
const [popupOpen, setPopupOpen] = useState(false);
20+
const [idToken, setIdToken] = useState("");
2021

2122
useEffect(() => {
2223
const initAuth0 = async () => {
@@ -36,6 +37,8 @@ export const Auth0Provider = ({
3637
if (isAuthenticated) {
3738
const user = await auth0FromHook.getUser();
3839
setUser(user);
40+
const idTokenClaims = await auth0FromHook.getIdTokenClaims();
41+
setIdToken(idTokenClaims.__raw);
3942
}
4043

4144
setLoading(false);
@@ -62,6 +65,9 @@ export const Auth0Provider = ({
6265
setLoading(true);
6366
await auth0Client.handleRedirectCallback();
6467
const user = await auth0Client.getUser();
68+
const idTokenClaims = await auth0Client.getIdTokenClaims();
69+
70+
setIdToken(idTokenClaims.__raw);
6571
setLoading(false);
6672
setIsAuthenticated(true);
6773
setUser(user);
@@ -83,7 +89,7 @@ export const Auth0Provider = ({
8389
}}
8490
>
8591
{children}
86-
<App />
92+
<App idToken={idToken} />
8793
</Auth0Context.Provider>
8894
);
89-
};
95+
};

todo-app-react/yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,6 +2026,14 @@ apollo-client@^2.6.10:
20262026
tslib "^1.10.0"
20272027
zen-observable "^0.8.0"
20282028

2029+
apollo-link-context@^1.0.20:
2030+
version "1.0.20"
2031+
resolved "https://registry.yarnpkg.com/apollo-link-context/-/apollo-link-context-1.0.20.tgz#1939ac5dc65d6dff0c855ee53521150053c24676"
2032+
integrity sha512-MLLPYvhzNb8AglNsk2NcL9AvhO/Vc9hn2ZZuegbhRHGet3oGr0YH9s30NS9+ieoM0sGT11p7oZ6oAILM/kiRBA==
2033+
dependencies:
2034+
apollo-link "^1.2.14"
2035+
tslib "^1.9.3"
2036+
20292037
apollo-link-http-common@^0.2.16:
20302038
version "0.2.16"
20312039
resolved "https://registry.yarnpkg.com/apollo-link-http-common/-/apollo-link-http-common-0.2.16.tgz#756749dafc732792c8ca0923f9a40564b7c59ecc"

0 commit comments

Comments
 (0)