I need to work with Graphql development server which requires from user Basic authentication.
On frontend side to make requests to protected graphql service I wrote next code
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
Authorization: 'Basic ' + btoa('<login>:<pass>'),
}
}
});
const httpLink = new HttpLink({
uri: process.env.REACT_APP_GQL_SERVER,
fetchOptions: {
mode: 'no-cors'
}
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
But I can't see "Authorization" header in the browser while request
Could you please support me to paste Authorization header to the request or understand another way to work with Default Browser Authentication Prompt.
using: "apollo-boost": "^0.1.22", "apollo-link-context": "^1.0.12",
=============================================
Tested variant to put header #1
=============================================
const httpLink = createHttpLink({
uri: process.env.REACT_APP_GQL_SERVER,
fetchOptions: {
mode: 'no-cors'
},
});
const middlewareLink = new ApolloLink((operation, forward: any) => {
operation.setContext({
headers: {
"Authorization": 'Basic ' + btoa('<login>:<password>')
}
});
return forward(operation);
});
const client = new ApolloClient({
link: middlewareLink.concat(httpLink),
cache: new InMemoryCache(),
});
=============================================
Tested variant to put header #2
=============================================
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
authorization: 'Basic ' + btoa('<login>:<password>'),
}
}
});
const httpLink = new HttpLink({
uri: process.env.REACT_APP_GQL_SERVER,
fetchOptions: {
mode: 'no-cors'
}
});
const links: any = [];
links.push(httpLink);
links.push(authLink);
const client = new ApolloClient({
link: ApolloLink.from(links),
cache: new InMemoryCache(),
});
=============================================
Tested variant to put header #3
=============================================
const middlewareLink = new ApolloLink((operation, forward: any) => {
operation.setContext({
headers: {
authorization: 'Basic ' + btoa('<login>:<password>')
}
});
return forward(operation);
});
const httpLink = new HttpLink({
uri: process.env.REACT_APP_GQL_SERVER,
fetchOptions: {
mode: 'no-cors'
}
});
const links: any = [];
links.push(httpLink);
links.push(middlewareLink);
const client = new ApolloClient({
link: ApolloLink.from(links),
cache: new InMemoryCache(),
});