-
Notifications
You must be signed in to change notification settings - Fork 267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use multi-auth with Apollo client #562
Comments
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 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(),
})
} |
Hi @boredcode, |
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
The text was updated successfully, but these errors were encountered: