【发布时间】:2021-01-11 06:39:58
【问题描述】:
我正在尝试用较新的 @aws-amplify 包填充我的 jwtToken,但事实证明这有点困难。
当尝试运行 Query 时,我会收到以下错误:Uncaught (in promise) No current user
我可以从源代码中看到,如果我将身份验证类型设置为AMAZON_COGNITO_USER_POOLS,那么我必须使用jwt token
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(), 按照上面代码中的要求返回了一个承诺......所以我不明白为什么这会失败?
_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
【问题讨论】:
标签: javascript reactjs amazon-web-services next.js apollo-client