简体   繁体   English

aws-amplify 身份验证...如何在成功的 Auth.signIn 上访问令牌?

[英]aws-amplify Authentication...how to access tokens on successful Auth.signIn?

I'm trying to figure out how to access the accessToken, refreshToken, and idToken that I receive back from aws-amplify using the Auth library.我正在尝试弄清楚如何使用 Auth 库访问从 aws-amplify 收到的 accessToken、refreshToken 和 idToken。

example in docs: https://aws.github.io/aws-amplify/media/authentication_guide.html文档中的示例: https://aws.github.io/aws-amplify/media/authentication_guide.html

example of my usage:我的用法示例:

const user = await Auth.signIn(email, password);

user has a bunch of properties that are inaccessible including everything I need. user有一堆无法访问的属性,包括我需要的一切。 In the docs, it's unclear how to get to these properties because the examples all log the result.在文档中,不清楚如何获取这些属性,因为示例都记录了结果。 Any ideas?有任何想法吗?

Auth.currentSession().then(res=>{
  let accessToken = res.getAccessToken()
  let jwt = accessToken.getJwtToken()
  //You can print them to see the full objects
  console.log(`myAccessToken: ${JSON.stringify(accessToken)}`)
  console.log(`myJwt: ${jwt}`)
})

Auth.currentSession() will return a CognitoUserSession containing accessToken , idToken , and refreshToken . Auth.currentSession()将返回包含CognitoUserSession accessTokenidTokenrefreshToken

The CognitoUserSession is actually the following: CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 0} CognitoUserSession实际上是这样的: CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 0}

Accessing pairs within that object can be achieved through straightforward dot notation at this point.此时可以通过简单的点符号来访问该对象内的对。


Example : Retrieve the accessToken and log to console示例:检索 accessToken 并登录到控制台

Auth.currentSession().then(data => console.log(data.accessToken));

The result will be a CognitoAccessToken in the form CognitoAccessToken { jwtToken: '', payload: ''}其结果将是在形式的CognitoAccessToken CognitoAccessToken { jwtToken: '', payload: ''}

If you just want the jwtToken within the CognitoAccessToken , it's just dot notation all the way down (with log to console example):如果您只想要jwtToken中的jwtToken ,它只是一直向下的点符号(带有日志到控制台示例):

Auth.currentSession().then(data => console.log(data.accessToken.jwtToken));

Note : This method also refreshes the current session if needed ( reference ).注意:如果需要,此方法还会刷新当前会话( 参考)。

I believe you can do我相信你能做到

Auth.currentCredentials(credentials => {
  const tokens = Auth.essentialCredentials(credentials);
})

where essentialCredentials will return all of the tokens其中, essentialCredentials将返回所有令牌

Hope this helps.希望这会有所帮助。

Angular 9, getting JWT token from current session : Angular 9,从当前会话获取 JWT 令牌:

import Auth from '@aws-amplify/auth';

Auth.currentSession().then(data => console.log("JWT", data.getAccessToken().getJwtToken()));

For those in search of the AWSCredentials :对于那些寻找AWSCredentials 的人

const checkCognitoUserSession = async () => {
  const getAwsCredentials = await Auth.currentCredentials();
  const awsCredentials = await Auth.essentialCredentials(getAwsCredentials);

  // accessKeyId, secretAccessKey, sessionToken post login
  return { awsCredentials };
};

Retrieve current session using aws-amplify使用 aws-amplify 检索当前 session

Auth.currentSession() returns a CognitoUserSession object which contains JWT accessToken, idToken, and refreshToken. Auth.currentSession() 返回一个 CognitoUserSession object,其中包含 JWT accessToken、idToken 和 refreshToken。

Auth.currentSession()
.then((data) => console.log(data))
.catch((err) => console.log(err));

aws-amplify Docs currentSession aws-amplify 文档currentSession

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Amplify + React 身份验证问题(在 Auth.signIn() 登录后未获得 JWT 值) - AWS Amplify + React Authentication Issue (Not getting JWT value after Auth.signIn() on Sign) 删除用户后 AWS Amplify Auth.signIn 仍在工作 - AWS Amplify Auth.signIn is still working after user is deleted @aws-amplify/auth federatedSignUp function? - @aws-amplify/auth federatedSignUp function? AWS Cognito:在 Auth.signIn 中出现错误(验证 amazon-cognito-identity-js 已链接) - AWS Cognito: Getting error in Auth.signIn (Validate that amazon-cognito-identity-js has been linked) aws-amplify auth currentSession 不返回当前用户 - aws-amplify auth currentSession returns no current user 无法从“aws-amplify/ui-react”导出 SignIn - Can't export SignIn from "aws-amplify/ui-react" 如何在反应单元测试中模拟 aws-amplify? - How to mock aws-amplify in react unit testing? AWS Amplify 错误从 aws-amplify 导入 StorageProvider 类 - AWS Amplify error import StorageProvider class from aws-amplify aws-amplify AWSIoTProvider 不是具有 Angular 8 的构造函数 - aws-amplify AWSIoTProvider is not a constructor with Angular 8 放大“npm install --save aws-amplify @aws-amplify/ui-angular”破坏了代码 - Amplify "npm install --save aws-amplify @aws-amplify/ui-angular" breaks the code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM