Open mdegrees opened 4 years ago
I'm using a similar hack except it's a little more cumbersome cos the fallback auth scheme I'm using is @aws_iam and not @ aws_api_key key which requires sigv4 signed headers.
+1
I used ApolloLink.split()
to decide which link to use
import { ApolloClient, ApolloLink, HttpLink } from '@apollo/client'
import { createAuthLink } from 'aws-appsync-auth-link'
let accessToken
// a function to be called when user login/out or when access token expires
export const setAccessToken = (token) => {
accessToken = token
}
function createApolloClient() {
// API key link
const apiAuthLink = createAuthLink({
auth: {
type: 'API_KEY',
apiKey: 'ABC123...',
},
})
// OpenID Connect link
const oidcAuthLink = createAuthLink({
auth: {
type: 'OPENID_CONNECT',
jwtToken: async () => accessToken,
},
})
// decide which the proper link from above to use (directional link)
const awsLink = ApolloLink.split((operation) => {
// use your own conditions here to decide which link to use. e.g. Auth.currentSession()
return (operation.operationName === `getUserDetails` || operation.variables.id === 1)
}, oidcAuthLink, apiAuthLink)
// http link (the terminating link in the chain)
const httpLink = new HttpLink({
uri: 'https://xxx.appsync.aws.com/graphql',
})
// create ApolloClient with AWS links and cache
return new ApolloClient({
link: ApolloLink.from([ awsLink, httpLink ]),
cache: new InMemoryCache(),
})
}
I'm using a similar hack except it's a little more cumbersome cos the fallback auth scheme I'm using is @aws_iam and not @ aws_api_key key which requires sigv4 signed headers.
Hi @boredcode, could you paste some code? I think I'll need to go for something similar..
When using Apollo client with Appsync, there seems to be no example showing the multi-auth scenario. On my schema I'm using
@aws_api_key @aws_cognito_user_pools
on multiples queries as in my use case some data is accessible to guests as well as to users.What is the current behavior? Currently you can either use API key or Cognito Pool as demonstrated here: https://github.com/awslabs/aws-mobile-appsync-sdk-js#using-authorization-and-subscription-links-with-apollo-client-no-offline-support
What I expected Providing both the api Key and Jwt Token with, AppSync picks the appropriate
What I have tried I tried the bellow hack but it seems too cumbersome + subscriptions do not work.
Thank you