如何在js代码中使用aws amplify实现Oauth以获取访问令牌?

问题描述 投票:0回答:1

我使用 aws 身份验证创建了一个 React 应用程序,

`从“aws-amplify”导入{放大}; 从“../aws-exports”导入 awsmobile; 从“@aws-amplify/ui-react”导入 { Authenticator, Flex }

Amplify.configure(awsmobile)

const 登录 = () => { 返回(

        <div>
            <Authenticator>
                {({ signOut, user }) => (
                    <div className="App">
                        <p onClick={() => { }}>
                            Hey {user.username}, welcome to my channel, with auth!
                        </p>
                        <button onClick={signOut}>Sign out</button>
                    </div>
                )}
            </Authenticator>
        </div>

)

} `

我的要求是开发其他应用程序,并且使用oauth,我需要来自该应用程序cognito的访问令牌

请给我一个解决方案

reactjs aws-amplify awscognitotoken
1个回答
0
投票

这是例子:

import { fetchAuthSession } from 'aws-amplify/auth';

async function currentSession() {
  try {
    const { accessToken, idToken } = (await fetchAuthSession()).tokens ?? {};
  } catch (err) {
    console.log(err);
  }
}

来自 Amplify 官方文档的更多详细信息:管理用户会话和凭据

© www.soinside.com 2019 - 2024. All rights reserved.