我试图使用更新的@aws-amplify
包填充我的jwtToken,但这证明是有些困难的。
当尝试运行一个Query
时,我会收到以下错误:Uncaught(在承诺中)没有当前用户
我可以从源代码中看到,如果将auth类型设置为AMAZON_COGNITO_USER_POOLS
,那么就必须使用jwt令牌
case AUTH_TYPE.AMAZON_COGNITO_USER_POOLS:
case AUTH_TYPE.OPENID_CONNECT:
const { jwtToken = '' } = auth;
promise = headerBasedAuth({ header: 'Authorization', value: jwtToken }, operation, forward);
这导致我尝试生成我的JWT令牌,这就是我的知识的瓶颈。我知道 jwtToken: async() => (await Auth.currentSession()).getIdToken().getJwtToken()
返回一个promise,正如上面的代码中所需的一样... 所以我不明白为什么会失败?
_app.js(next.js)
import Amplify from '@aws-amplify/core'
import { Auth } from '@aws-amplify/auth'
import { ApolloProvider } from '@apollo/react-hooks'
import { ApolloLink } from 'apollo-link'
import { createAuthLink } from 'aws-appsync-auth-link'
import { InMemoryCache, ApolloClient } from '@apollo/client'
import { createHttpLink } from 'apollo-link-http'
import awsExports from '../aws-exports'
Amplify.configure(awsExports)
Auth.configure(awsExports)
const url = awsExports.aws_appsync_graphqlEndpoint
const region = awsExports.aws_appsync_region
const auth = {
type: awsExports.aws_appsync_authenticationType,
jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken(),
}
const link = ApolloLink.from([createAuthLink({ url, region, auth }), createHttpLink({ uri: url })])
const client = new ApolloClient({
link,
cache: new InMemoryCache(),
})
const MyApp = function ({ Component, pageProps, router }) {
return (
.....
<ApolloProvider client={client}>
.....
)
}
export default MyApp