Course lesson

Set Dynamic Auth Header for Apollo Client using Next.js getServerSideProps

To make an authenticated request, we will need to get logged in users auth tokens from Auth0. Auth0 exposes a getAccessToken function that we can use in Next.js getServerSideProps to fetch the user token. It's important to note that this function can only be...

Duration
3 min
Access
Free
Transcript
Retained from source evidence

To make an authenticated request, we will need to get logged in users auth tokens from Auth0. Auth0 exposes a getAccessToken function that we can use in Next.js getServerSideProps to fetch the user token. It's important to note that this function can only be used server side. We'll pass that token into the page we need to send authed requests.

To enable Apollo Client to set the headers we'll extend the client so that allows us to pass in auth tokens and sets the client context. We will then destructure the client from our useMutation hook and set the context to include the user token.