1

Im Using User Pool, and is possible develop a api to communicate with AWS Cognito? or just to communicate with front-end apps (android, ios, javascript web)?

And if is possible, how can I identify the user logged and get their informations?

UPDATE 1: To get decode jwt informations: https://github.com/awslabs/aws-support-tools/tree/master/Cognito/decode-verify-jwt

UPDATE 2: Cognito is better to use on front-end, because you need store 'cognitoUser' object. And the methods is using this object.

UPDATE 3: You can do the login on backend, and use the token to use API Gateway on AWS. But the problem is if you need update something on Cognito. If you need, you can use the admin methods (https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/Welcome.html).

TitaoYamamoto
  • 325
  • 1
  • 5
  • 17
  • Yes it is possible, and I have a module already maid for it, with this module you can implement authentication using google, facebook, amazon in few hours say 5 to 6 hours. If you need more details, email me iamtusharsb@gmail.com – hashed_name Jun 15 '18 at 06:46

2 Answers2

1

Yes of course it's possible, here is a sample example to use cognito with facebook login on node : https://gist.github.com/pcoady/8afab5defb89f1900c9b

You can also check this step by step tutorial from backspace.academy (3 parts)

From another stackoverflow answer :

The ID Token that you exchange with Cognito federated identity service to get the identity id and credentials already has all user attributes. You do not need an extra call to any service.

It is a JWT token and you can use any library on the client to decode the values. You can read this guide for more information about the tokens vended by Cognito user pools.

Alternatively, you can also use the Access Token to call GetUser API which will return all the user information.

You can check this code to retrieve the cognito IDToken with nodejs

Community
  • 1
  • 1
Palisanka
  • 447
  • 4
  • 10
  • Im using User Pool, (without social media yet). But do you know how can I identify the user logged and get their informations? I can get their with token? I really don´t know. – TitaoYamamoto Jun 15 '18 at 14:06
  • I updated my answer, you should be able to get the attributes with `cognitoUser.getUserAttributes` I know this code works for frontend but you perhaps will have to adapt it to nodejs – Palisanka Jun 15 '18 at 14:17
  • Thanks for your help. Yes, this code if for frontend (Retrieve the current user from local storage). Im trying to get this on Node, Im looking for search the user about token or some else field. If you know another way, please, let me know. Thanks again. – TitaoYamamoto Jun 15 '18 at 14:22
  • Usually the users informations are inside the IDToken, once you have the token, you should be able to retrieve the attributes – Palisanka Jun 15 '18 at 14:31
  • you can check this code to retrieve the idtoken with node : https://gist.github.com/kidsil/cb0112e912960f517d88c586e333bdc3 – Palisanka Jun 15 '18 at 14:38
  • Using 'amazon-cognito-identity-js' and 'cognitoUser.authenticateUser' (with login and password) I can get the jwtToken (result.getAccessToken().getJwtToken()), but how can I get user informations? I try decode jwt (jwt_decode(result.getAccessToken().getJwtToken())) but I receive { code: 'UnknownError', message: 'Unkown error' }. And how can update user attributes without login and password? Just with token or just another field? because I need the 'cognitoUser' to do 'cognitoUser.updateAttributes'. And thaks again. You are really helping-me. – TitaoYamamoto Jun 15 '18 at 15:55
  • Since you validated my answer did you succeed ? Just logging the Token should be enough to see the attributes – Palisanka Jun 18 '18 at 14:56
  • Yes, I updated my question and put more informations. Thanks @Palisanka – TitaoYamamoto Jun 18 '18 at 16:35
0

Yes, it is possible you can use amazon-cognito-identity-js library.

here is the simple way to configure it.

const { CognitoUserPool } = require('amazon-cognito-identity-js');
const userPool = {
  UserPoolId: // userpoolId,
  ClientId: // clientId,
};
module.exports = new CognitoUserPool(userPool);

and here is the signup code:

const userPool = require('./config/awsUserPool');
function SignUp(email, password) {
  return new Promise((resolve, reject) => {
    userPool.signUp(email, password, [], null, (err, result) => {
      if (err) {
        return reject(err);
      }
      const { user, userConfirmed, userSub } = result;
      return resolve({ user, userConfirmed, userSub });
    });
  });
}
module.exports = SignUp;