Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- const login = () => {
- let Username = 'reacttest';
- let Password = 'reacttest';
- let authenticationDetails = new AuthenticationDetails({
- Username,
- Password
- });
- const { UserPoolId, ClientId, region } = config;
- var poolData = { UserPoolId, ClientId, region };
- var userPool = new CognitoUserPool(poolData);
- var userData = {
- Username : 'reacttest',
- Pool : userPool
- };
- var cognitoUser = new CognitoUser(userData);
- let sessionData;
- if (cognitoUser != null) {
- cognitoUser.getSession(function(err, session) {
- if (err) {
- alert(err);
- return;
- };
- sessionData = session;
- console.log('sessionData', sessionData);
- console.log('session validity: ' + session.isValid());
- });
- }
- if (sessionData)
- console.log('idToken =>>>', sessionData.idToken.getJwtToken());
- cognitoUser.authenticateUser(authenticationDetails, {
- onSuccess: function (result) {
- // get identity token
- // identityPoolId and poolData coming from config.js
- var params = {
- IdentityPoolId: config.IdentityPoolId,
- Logins: {
- [`cognito-idp.${config.region}.amazonaws.com/${config.UserPoolId}`]:
- result.getIdToken().getJwtToken()
- }
- };
- // get credentials
- AWS.config.credentials = new AWS.CognitoIdentityCredentials(params, function(err, data) {
- if (err) console.log(err, err.stack);
- else console.log('AWS.config.credentials', data);
- });
- AWS.config.credentials.get(function(err){
- if (err) {
- alert(err);
- } else {
- var accessKeyId = AWS.config.credentials.accessKeyId;
- var secretAccessKey = AWS.config.credentials.secretAccessKey;
- var sessionToken = AWS.config.credentials.sessionToken;
- console.log('accessKeyId =>>>', accessKeyId);
- console.log('secretKey =>>>', secretAccessKey);
- console.log('sessionToken =>>>', sessionToken);
- console.log('region =>>>', poolData.region);
- // eslint-disable-next-line
- var apigClient = apigClientFactory.newClient({
- accessKey: accessKeyId,
- secretKey: secretAccessKey,
- sessionToken: sessionToken,
- region: poolData.region
- });
- // here we could add an api call
- var params = {};
- var additionalParams = {
- headers: {
- // Authorization: sessionData.idToken
- Authorization: sessionData.idToken.getJwtToken()
- },
- queryParams: {}
- };
- var body = {};
- apigClient.identityGet(params, body, additionalParams)
- .then(function(result){
- if (err) {
- alert(err);
- } else {
- console.log(JSON.stringify(result.data));
- }
- }).catch( function(result){
- //This is where you would put an error callback
- if (err) {
- alert(err);
- }
- });
- }
- });
- },
- onFailure: function(err) {
- alert(err);
- },
- });
- };
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement