diff --git a/src/app/apolloClient.js b/src/app/apolloClient.js index 3df1923..9c3ebdd 100644 --- a/src/app/apolloClient.js +++ b/src/app/apolloClient.js @@ -3,7 +3,10 @@ import { setContext } from "@apollo/client/link/context"; import { createPersistedQueryLink } from "apollo-link-persisted-queries"; import cachedZones from "./cached-data/zones.json"; -import { readCypressLoginData } from "./components/useCurrentUser"; +import { + getAuthModeFeatureFlag, + readCypressLoginData, +} from "./components/useCurrentUser"; // Teach Apollo to load certain fields from the cache, to avoid extra network // requests. This happens a lot - e.g. reusing data from item search on the @@ -176,6 +179,18 @@ const buildAuthLink = (getAuth0) => } }); +// This is a temporary way to pass the DTIAuthMode feature flag back to the +// server! +const authModeLink = setContext((_, { headers = {} }) => { + const authMode = getAuthModeFeatureFlag(); + return { + headers: { + ...headers, + "DTI-Auth-Mode": authMode, + }, + }; +}); + async function getAccessToken(getAuth0) { // Our Cypress tests store login data separately. Use it if available! const cypressToken = readCypressLoginData()?.encodedToken; @@ -210,6 +225,7 @@ for (const zone of cachedZones) { const buildLink = (getAuth0) => buildAuthLink(getAuth0) + .concat(authModeLink) .concat( createPersistedQueryLink({ useGETForHashedQueries: true, diff --git a/src/app/components/LoginModal.js b/src/app/components/LoginModal.js index 65cf34e..354dabc 100644 --- a/src/app/components/LoginModal.js +++ b/src/app/components/LoginModal.js @@ -58,14 +58,27 @@ function LoginForm({ onSuccess }) { const [ sendLoginMutation, { loading, error, data, called, reset }, - ] = useMutation(gql` - mutation LoginForm_Login($username: String!, $password: String!) { - login(username: $username, password: $password) { - id - username + ] = useMutation( + gql` + mutation LoginForm_Login($username: String!, $password: String!) { + login(username: $username, password: $password) { + id + } } + `, + { + update: (cache) => { + // Evict the `currentUser` from the cache, which will force all queries + // on the page that depend on it to update. (This includes the + // GlobalHeader that shows who you're logged in as!) + // + // I don't do any optimistic UI here, because auth is complex enough + // that I'd rather only show login success after validating it through + // an actual server round-trip. + cache.evict({ id: "ROOT_QUERY", fieldName: "currentUser" }); + }, } - `); + ); return (