DEV Community

Discussion on: Using Amplify CLI for adding Auth to your Hasura GraphQL engine

Collapse
csatterlee profile image
csatterlee • Edited on

Thanks for the great tutorial. I managed to get this working easily, but I'm having a lot of trouble with the next step, which is connecting my newly authenticated app to Hasura via Apollo. The token I get works fine if I paste it into Hasura, but when I set up Apollo to send requests to Hasura I get the following error:

Error: GraphQL error: Malformed Authorization header

I don't know what the problem is or whether it's Hasura or Apollo or Cognito, but if I console log the jwt before passing it to Apollo I can paste it into Hasura and it works fine. Is there a step I'm missing? Here's my code, pretty much right out of the Apollo docs example:

async function getSession() {
Auth.currentSession().then(res=>{
let idToken = res.getIdToken()
let jwt = idToken.getJwtToken()
//You can print them to see the full objects
console.log(JSON.stringify(idToken))
console.log(jwt)
return jwt
})
}

const token = getSession()

const client = new ApolloClient({
uri: 'api.example.com/v1/graphql',
headers: {
authorization: token ? Bearer ${token} : "",

}});

//backtics don't show in headers section

Collapse
vladimirnovick profile image
Vladimir Novick Author

getSession is async function and you missed await keyword calling getSession. Your token is a promise.