Get client data through AWS cognito authuntication code (PHP)

0

Hi,

My client portal is written in PHP 7.3 and access manage by Cognito. I need to get client details after successful login. I managed to get the access token and the id token using the authorization code. But, unsuccess in getting user data using those codes. I need recommended PHP code to obtain client data.

Regards,

Sanka

Sanka
asked 8 months ago399 views
2 Answers
0
Accepted Answer
Sanka
answered 8 months ago
0

Hi,

Recommended way to access AWS services with PHP is via AWS PHP SDK.

Specifically for Cognito: https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-cognitoidentity.html

Available Cognito APIs: https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-cognitoidentity.html#api-reference

ListIdentities and DescribeIdentity are the APIs that you need.

Best,

Didier

profile pictureAWS
EXPERT
answered 8 months ago
  • Hi Didier,

    Thanks for the answer. The reference doesn't explain how to get the client data out. My code segment as follows.

    if ($responseBody!=null){ $tokenData = json_decode($responseBody, true); $accessToken = $tokenData['access_token']; $idToken = $tokenData['id_token']; error_log("----2 idToken : ".$idToken); error_log("----2 accessToken : ".$accessToken);

    if ($accessToken!=null){ $credentials = [ 'key' => $accessToken, 'secret' => $clientSecret, 'token' => $idToken, // Only needed for temporary session tokens (e.g., with AssumeRole) 'verify' => false, ]; $cognitoClient = new CognitoIdentityProviderClient([ 'version' => 'latest', 'region' => 'eu-west-2', 'credentials' => $credentials, 'verify' => false, ]);

    try {
      $user = $cognitoClient->getUser([
          'AccessToken' => $accessToken,
          'verify' => false,
      ]);
      error_log("----2 getUser 2 ");
      $userAttributes = $result['UserAttributes'];
      print_r($userAttributes);
    } catch (CognitoIdentityProviderException $e) {
      error_log("----2 getUser CognitoIdentityProviderException ");
      var_dump($e);
    }catch (Exception $e) {
      error_log("----2 getUser Exception ");
      var_dump($e);
    }
    

    -

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions