0

I am learning to create a serverless API server using AWS lambda, dynamodb, cogito sync. It was going well until I got confused with users table.

So basically, I am trying to make twitter clone API. So as user I should be able to create post, follow another users etc.

Signup and Signin are successfully handled by Cognito Identity, the problem is how do I access the Users data on the cognito? A user can have following and followers attributes which contains other users ID.

What I did currently, on the app I register using cognito identity then I will make another call to the API gateway to create a user on dynamodb. So basically there are two separate users data. I am not sure if this is the correct way to do this.

Should I make a call on cognito on the backend instead on the app? Should i have separate users table for this?

Example Front End code on ionic

$scope.signup = function() {
  $ionicLoading.show({
    template: 'Registering user...'
  });
  // this is the factory for signing up
  awsCognitoIdentityFactory.signUp($scope.user.email, $scope.user.name, $scope.user.email, $scope.user.password,
    function(err, result) {
      if (err) {
        errorHandler(err);
        return false;
      }
      // creating user on the api
      User.create($scope.user).then(function(response) {
        console.log(response);
      });
      // store locally
      store.set('user', $scope.user);

      $ionicLoading.hide();
      $scope.$apply();

      $scope.user = {}; //clear register form
      $state.go('confirmation');
    });
  return true;
};
danggrianto
  • 371
  • 1
  • 4
  • 11

1 Answers1

0

It sounds like you're using Cognito User Pools, in which case you can use the GetUser API. Given context for the user, it will return all attributes stored against that user.

I can't comment on the best way to store user metadata as it will vary greatly based on the specifics and needs of your app, but it's probably worth reading up on Cognito's system of custom attributes, which lets you store custom data against users. These can be configured/added from the Cognito console.

Jeff Bailey
  • 5,655
  • 1
  • 22
  • 30
  • yes, correct I am user cognito user pools. That `GetUser API` that you mentioned is only works when I am getting current user right? What about if I want to see other people profile? Also I checked that the custom attributes only stores `String | Number | DateTime | Boolean` What if i want to store an Array such as `followers` or `friends`? – danggrianto Dec 12 '16 at 03:51
  • That is correct, a user can't use that on another user. There's an admin version of that API, however that ideally wouldn't be used in the application itself. That's roughly what I meant by depending on your apps needs. Cognito doesn't support the use case of one user seeing another's attributes, but that could be done with an external data store augmenting what Cognito already does. A set of followers/friends could be done with the username or 'sub' (unique user identifier) in an array, serialized to a string. That could also be put in an external data store as needed, though. – Jeff Bailey Dec 12 '16 at 04:00