const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
const poolData = {
UserPoolId: 'USER_POOL_ID_HERE',
ClientId: 'APP_CLIENT_ID_HERE'
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
const authenticationData = {
Username: 'USER_EMAIL_HERE',
Password: 'USER_PASSWORD_HERE',
};
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
const userData = {
Username: 'USER_EMAIL_HERE',
Pool: userPool
};
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
const idToken = result.getIdToken().getJwtToken();
localStorage.setItem('id_token', idToken);
},
onFailure: function (err) {
alert(err.message || JSON.stringify(err));
}
});
const idToken = localStorage.getItem('id_token');
fetch('https://api.example.com/protected-resource', {
headers: new Headers({
'Authorization': 'Bearer ' + idToken
})
})
上一篇:AWSCognitoRefreshTokenAPI总是显示SecretHash与客户端不匹配
下一篇:AWSCognitoRespondtoNew_Password_Requiredchallenge返回无法修改已提供的电子邮件