· 8 years ago · May 12, 2017, 12:36 AM
1```
2 const login = () => {
3
4 let Username = 'reacttest';
5 let Password = 'reacttest';
6
7 let authenticationDetails = new AuthenticationDetails({
8 Username,
9 Password
10 });
11
12 const { UserPoolId, ClientId } = config;
13 var poolData = { UserPoolId, ClientId };
14 var userPool = new CognitoUserPool(poolData);
15 var userData = {
16 Username : 'reacttest',
17 Pool : userPool
18 };
19 var cognitoUser = new CognitoUser(userData);
20
21 cognitoUser.authenticateUser(authenticationDetails, {
22 onSuccess: function (result) {
23
24 AWS.config.credentials = new AWS.CognitoIdentityCredentials({
25 IdentityPoolId : config.IdentityPoolId, // your identity pool id here
26 Logins : {
27 ['cognito-idp.' + config.region + '.amazonaws.com/' + config.UserPoolId] : result.getIdToken().getJwtToken()
28 }
29 });
30
31 AWS.config.credentials.refresh(function(){
32 console.log(AWS.config.credentials);
33 var apigClient = window.apigClientFactory.newClient({
34 accessKey: AWS.config.credentials.accessKeyId,
35 secretKey: AWS.config.credentials.secretAccessKey,
36 sessionToken: AWS.config.sessionToken, //OPTIONAL: If you are using temporary credentials you must include the session token
37 region: 'eu-west-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1
38 });
39 var additionalParams = {
40 headers: {
41 "Access-Control-Allow-Origin": "*",
42 "Access-Control-Allow-Methods": "*",
43 "Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token"
44
45 },
46 };
47 apigClient.identityiamGet({},{},additionalParams).then(res => {
48 console.log(res);
49 })
50
51 });
52
53 },
54 onFailure: function(err) {
55 alert(err);
56 },
57 });
58 };
59```