我有一台具有Apache Basic Auth设置的Prisma服务器,在React-Native中可以完美运行...但是在我们的React.js项目中,永远不会添加auth标头。
import React, {Component} from 'react'
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { ApolloProvider } from 'react-apollo'
import Home from './Container'
const httpLink = createHttpLink({
uri: 'https://app.url/graphql',
fetchOptions: {
mode: 'no-cors',
},
})
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
({
headers: {
Authorization: 'Basic base64goeshere',
...headers,
},
})
)
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
})
export default class WithProvider extends Component {
render() {
return (
<ApolloProvider client={client}>
<Home auth={this.props.auth} />
</ApolloProvider>
)
}
}
没有身份验证标头,并且由于缺少标头而导致401错误。