Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/use fresh tokens #1627

Merged
merged 13 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/auth/src/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export interface AuthContextInterface {
/* Determining your current authentication state */
loading: boolean
isAuthenticated: boolean
authToken: string | null
/**
* @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
/* The current user's data from the `getCurrentUser` function on the api side */
currentUser: null | CurrentUser
/* The user's metadata from the auth provider */
Expand Down Expand Up @@ -55,7 +58,7 @@ export interface AuthContextInterface {
export const AuthContext = React.createContext<AuthContextInterface>({
loading: true,
isAuthenticated: false,
authToken: null,
authToken: null, // @WARN! deprecated, will always be null
userMetadata: null,
currentUser: null,
})
Expand All @@ -69,7 +72,7 @@ type AuthProviderProps = {
type AuthProviderState = {
loading: boolean
isAuthenticated: boolean
authToken: string | null
authToken: string | null // @WARN! deprecated, will always be null
userMetadata: null | Record<string, any>
currentUser: null | CurrentUser
hasError: boolean
Expand All @@ -96,7 +99,7 @@ export class AuthProvider extends React.Component<
state: AuthProviderState = {
loading: true,
isAuthenticated: false,
authToken: null,
authToken: null, // @WARN! deprecated, will always be null
userMetadata: null,
currentUser: null,
hasError: false,
Expand All @@ -115,14 +118,16 @@ export class AuthProvider extends React.Component<
}

getCurrentUser = async (): Promise<Record<string, unknown>> => {
// 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`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
'auth-provider': this.rwClient.type,
authorization: `Bearer ${this.state.authToken}`,
authorization: `Bearer ${token}`,
},
body: JSON.stringify({
query:
Expand Down Expand Up @@ -173,15 +178,13 @@ export class AuthProvider extends React.Component<
}

getToken = async () => {
const authToken = await this.rwClient.getToken()
this.setState({ ...this.state, authToken })
return authToken
return this.rwClient.getToken()
}

reauthenticate = async () => {
const notAuthenticatedState: AuthProviderState = {
isAuthenticated: false,
authToken: null,
authToken: null, // @WARN! deprecated, will always be null
currentUser: null,
userMetadata: null,
loading: false,
Expand Down
15 changes: 14 additions & 1 deletion packages/auth/src/__tests__/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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'
Expand Down Expand Up @@ -41,9 +43,9 @@ const AuthConsumer = () => {
const {
loading,
isAuthenticated,
authToken,
logOut,
logIn,
getToken,
userMetadata,
currentUser,
reauthenticate,
Expand All @@ -52,6 +54,17 @@ const AuthConsumer = () => {
error,
} = useAuth()

const [authToken, setAuthToken] = useState(null)

const retrieveToken = async () => {
const token = await getToken()
setAuthToken(token)
}

useEffect(() => {
retrieveToken()
})
dac09 marked this conversation as resolved.
Show resolved Hide resolved
peterp marked this conversation as resolved.
Show resolved Hide resolved

if (loading) {
return <>Loading...</>
}
Expand Down
43 changes: 40 additions & 3 deletions packages/web/src/components/RedwoodApolloProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import {
ApolloProvider,
ApolloClientOptions,
ApolloClient,
ApolloLink,
InMemoryCache,
useQuery,
useMutation,
createHttpLink,
} from '@apollo/client'
import { setContext } from '@apollo/client/link/context'

import type { AuthContextInterface } from '@redwoodjs/auth'
import { AuthContextInterface, useAuth } from '@redwoodjs/auth'

import {
FetchConfigProvider,
Expand All @@ -20,12 +23,46 @@ const ApolloProviderWithFetchConfig: React.FunctionComponent<{
config?: Omit<ApolloClientOptions<InMemoryCache>, 'cache'>
}> = ({ config = {}, children }) => {
const { uri, headers } = useFetchConfig()
const { getToken, type: authProviderType, isAuthenticated } = useAuth()

const withToken = setContext(async () => {
if (isAuthenticated && getToken) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case when getToken would be falsy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing the failure in the e2e test. Its falsy when you're not logged in - wasn't sure if there's any other case where its falsy apart from the first render of the auth provider.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getToken is falsey? But surely that shouldn't be the case since the function is provided via the provider? Maybe one of the "fake implementations" doesn't follow the providers spec.

const token = await getToken()

return { token }
}

return { token: null }
})

const authMiddleware = new ApolloLink((operation, forward) => {
const { token } = operation.getContext()

// Only add auth headers when token is present
// Token is null, when !isAuthenticated
const authHeaders = token
? {
'auth-provider': authProviderType,
authorization: `Bearer ${token}`
}
: {}

operation.setContext(() => ({
headers: {
...headers,
// Duped auth headers, because we may remove FetchContext at a later date
...authHeaders,
},
}))
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 <ApolloProvider client={client}>{children}</ApolloProvider>
Expand Down