From 8399f547a0e7bb103383c407647953c579378f6d Mon Sep 17 00:00:00 2001 From: David Price Date: Thu, 7 Jan 2021 10:00:49 -0800 Subject: [PATCH] Revert "Configure ApolloProvider to always use fresh tokens (#1609)" (#1611) This reverts commit ef14ece2d9c2cc8755ac76867e081a9b3193d95f. --- packages/auth/src/AuthProvider.tsx | 21 ++++++------- .../auth/src/__tests__/AuthProvider.test.tsx | 15 +--------- .../src/components/RedwoodApolloProvider.tsx | 30 ++----------------- 3 files changed, 13 insertions(+), 53 deletions(-) diff --git a/packages/auth/src/AuthProvider.tsx b/packages/auth/src/AuthProvider.tsx index 2cffda95b2c6..2b6447a5af09 100644 --- a/packages/auth/src/AuthProvider.tsx +++ b/packages/auth/src/AuthProvider.tsx @@ -16,10 +16,7 @@ export interface AuthContextInterface { /* Determining your current authentication state */ loading: boolean isAuthenticated: boolean - /** - * @deprecated auth tokens are now refreshed when they expire, use getToken() instead. authToken will be removed from this context in future releases - */ - authToken: string | null // @WARN! deprecated, will always be null + authToken: string | null /* The current user's data from the `getCurrentUser` function on the api side */ currentUser: null | CurrentUser /* The user's metadata from the auth provider */ @@ -58,7 +55,7 @@ export interface AuthContextInterface { export const AuthContext = React.createContext({ loading: true, isAuthenticated: false, - authToken: null, // @WARN! deprecated, will always be null + authToken: null, userMetadata: null, currentUser: null, }) @@ -72,7 +69,7 @@ type AuthProviderProps = { type AuthProviderState = { loading: boolean isAuthenticated: boolean - authToken: string | null // @WARN! deprecated, will always be null + authToken: string | null userMetadata: null | Record currentUser: null | CurrentUser hasError: boolean @@ -99,7 +96,7 @@ export class AuthProvider extends React.Component< state: AuthProviderState = { loading: true, isAuthenticated: false, - authToken: null, // @WARN! deprecated, will always be null + authToken: null, userMetadata: null, currentUser: null, hasError: false, @@ -118,8 +115,6 @@ export class AuthProvider extends React.Component< } getCurrentUser = async (): Promise> => { - // Always get a fresh token, rather than use the one in state - const token = await this.getToken() const response = await window.fetch( `${window.__REDWOOD__API_PROXY_PATH}/graphql`, { @@ -127,7 +122,7 @@ export class AuthProvider extends React.Component< headers: { 'content-type': 'application/json', 'auth-provider': this.rwClient.type, - authorization: `Bearer ${token}`, + authorization: `Bearer ${this.state.authToken}`, }, body: JSON.stringify({ query: @@ -178,13 +173,15 @@ export class AuthProvider extends React.Component< } getToken = async () => { - return this.rwClient.getToken() + const authToken = await this.rwClient.getToken() + this.setState({ ...this.state, authToken }) + return authToken } reauthenticate = async () => { const notAuthenticatedState: AuthProviderState = { isAuthenticated: false, - authToken: null, // @WARN! deprecated, will always be null + authToken: null, currentUser: null, userMetadata: null, loading: false, diff --git a/packages/auth/src/__tests__/AuthProvider.test.tsx b/packages/auth/src/__tests__/AuthProvider.test.tsx index e1ee2dab1885..8ea14eb37eaf 100644 --- a/packages/auth/src/__tests__/AuthProvider.test.tsx +++ b/packages/auth/src/__tests__/AuthProvider.test.tsx @@ -1,7 +1,5 @@ require('whatwg-fetch') -import { useEffect, useState } from 'react' - import { render, screen, fireEvent, waitFor } from '@testing-library/react' import '@testing-library/jest-dom/extend-expect' import { graphql } from 'msw' @@ -43,9 +41,9 @@ const AuthConsumer = () => { const { loading, isAuthenticated, + authToken, logOut, logIn, - getToken, userMetadata, currentUser, reauthenticate, @@ -54,17 +52,6 @@ const AuthConsumer = () => { error, } = useAuth() - const [authToken, setAuthToken] = useState(null) - - const retrieveToken = async () => { - const token = await getToken() - setAuthToken(token) - } - - useEffect(() => { - retrieveToken() - }) - if (loading) { return <>Loading... } diff --git a/packages/web/src/components/RedwoodApolloProvider.tsx b/packages/web/src/components/RedwoodApolloProvider.tsx index 6ade5050ead7..83f678c2b984 100644 --- a/packages/web/src/components/RedwoodApolloProvider.tsx +++ b/packages/web/src/components/RedwoodApolloProvider.tsx @@ -2,15 +2,12 @@ import { ApolloProvider, ApolloClientOptions, ApolloClient, - ApolloLink, InMemoryCache, useQuery, useMutation, - createHttpLink, } from '@apollo/client' -import { setContext } from '@apollo/client/link/context' -import { AuthContextInterface, useAuth } from '@redwoodjs/auth' +import type { AuthContextInterface } from '@redwoodjs/auth' import { FetchConfigProvider, @@ -23,33 +20,12 @@ const ApolloProviderWithFetchConfig: React.FunctionComponent<{ config?: Omit, 'cache'> }> = ({ config = {}, children }) => { const { uri, headers } = useFetchConfig() - const { getToken, type: authProviderType } = useAuth() - - const withToken = setContext(async () => { - const token = await getToken() - return { token } - }) - - const authMiddleware = new ApolloLink((operation, forward) => { - const { token } = operation.getContext() - - operation.setContext(() => ({ - headers: { - ...headers, - // Duped auth headers, because we may remove FetchContext at a later date - 'auth-provider': authProviderType, - authorization: token ? `Bearer ${token}` : null, - }, - })) - return forward(operation) - }) - - const httpLink = createHttpLink({ uri }) const client = new ApolloClient({ cache: new InMemoryCache(), + uri, + headers, ...config, - link: ApolloLink.from([withToken, authMiddleware.concat(httpLink)]), }) return {children}