# User authentication

Now that users can sign up, let's see how do send the token with all requests.

We need to remove `apollo-boost` to start using `apollo-link`.

{% code title="\~/packages/web" %}

```
$ yarn remove apollo-boost
$ yarn add apollo-link-http apollo-link-context apollo-cache-inmemory graphql-tag
```

{% endcode %}

In `App.tsx` replace:

```typescript
import ApolloClient from "apollo-boost";
const client = new ApolloClient({ uri: "/graphql" });
```

with:

```typescript
import ApolloClient from "apollo-client";
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';

const httpLink = createHttpLink({
  uri: '/graphql',
});

const authLink = setContext((_, { headers }) => {
  // get the authentication token from local storage if it exists
  const token = localStorage.getItem('token');
  // return the headers to the context so httpLink can read them
  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : "",
    }
  }
});

const client = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache()
});
```

Also replace `import { gql } from "apollo-boost"` with`import gql from "graphql-tag";`

To see if this is working we can log out `context` in our `Health` resolver:

{% code title="resolvers/Health.ts" %}

```typescript
@Resolver(of => Health)
class HealthResolver {
  @Query(returns => Health)
  async health(@Ctx() context: GraphQLContext): Promise<Health> {
    console.log({ context })
    return { ok: 1 };
  }
}
```

{% endcode %}

`LinksPage.tsx` is still sending a query to `Health.` You should see the following in your terminal:

```
{
  context: {
    user: { id: '9cd7e02b-0333-4d67-a43b-6957fb38caac', iat: 1570621069 }
  }
}
```

{% hint style="info" %}
[`send-token`](https://github.com/florianherrengt/book-code/tree/send-token)branch available on GitHub.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tutorial.specian.co.uk/frontend/user-authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
