[英]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 accessToken
, idToken
和refreshToken
。
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.