Progress with authentication tolen

This commit is contained in:
Riccardo
2021-01-05 13:58:26 +01:00
parent 4d021d6043
commit 436eb4410c
8 changed files with 138 additions and 34 deletions

View File

@@ -1,8 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './styles/index.css';
import { AUTH_TOKEN } from './constants';
import App from './components/App';
import { BrowserRouter } from 'react-router-dom';
import { setContext } from '@apollo/client/link/context';
// import * as serviceWorker from './serviceWorker';
import {
@@ -16,8 +18,19 @@ const httpLink = createHttpLink({
uri: 'http://localhost:4000/graphql'
});
const authLink = setContext((_, { headers }) => {
const token = localStorage.getItem(AUTH_TOKEN);
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
}
};
});
const client = new ApolloClient({
link: httpLink,
link: authLink.concat(httpLink),
// link: httpLink,
cache: new InMemoryCache()
});