ellaisys / aws-cognito

AWS Cognito package (with MFA Feature) using the AWS SDK for PHP/Laravel
https://ellaisys.github.io/aws-cognito/
MIT License
107 stars 42 forks source link

`createCognitoUser` returns the AWS Cognito User instead of `true` #71

Closed damonjentree closed 9 months ago

damonjentree commented 10 months ago

Is your feature request related to a problem? Please describe. I would like to suggest a feature—specifically when registering new Cognito users.

I am using AWS Cognito as my authentication provider. I'm able to successfully create a user within Cognito, but would like the users' sub (uuid) returned so that I can use that within my Laravel application. This way the user_id within my Laravel application matches the Username (or sub) in Cognito. FWIW, I am using laravel as an API.

Describe the solution you'd like When calling createCognitoUser(), the aws instance is returned instead of true. For example, a sample of the response:

"Username" => "84583468-8081-7000-901f-9d3de5b6b68b"
      "Attributes" => array:6 [
        0 => array:2 [
          "Name" => "sub"
          "Value" => "84583468-8081-7000-901f-9d3de5b6b68b"
        ]
        1 => array:2 [
          "Name" => "email_verified"
          "Value" => "true"
        ]
        2 => ...

Describe alternatives you've considered I've tried implementing the register() method, but do not wish to provide a password. I'd like Cognito to generate a temporary password just as it does when calling createCognitoUser().

Additional context I have updated this file: vendor/ellaisys/aws-cognito/src/AwsCognitoClient.php This is right around line #468. Here I am returning the user that is created in Cognito. I'm also throwing an exception if the username exists.

use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

...

try {
    $user = $this->client->adminCreateUser($payload);

    //Add user to the group
    if (!empty($groupname)) {
        $this->adminAddUserToGroup($username, $groupname);
    }
} catch (CognitoIdentityProviderException $e) {
    throw new BadRequestHttpException($e->getAwsErrorMessage());  // "message": "An account with the given email already exists.",
}

 ...

Returning the $user above is returning me the AWS Cognito user. From there, I'm able to do something like this:

$user = new User([
    'given_name' => $request->given_name,
    'family_name' => $request->family_name,
    'email' => $request->email,
    'phone_number' => $request->phone_number,
]);

$user->id = $response['User']['Username'];  // 84583468-8081-7000-901f-9d3de5b6b68b
$user->save();
amitdhongde commented 10 months ago

Thanks @damonjentree. We will surely look into this request.

amitdhongde commented 9 months ago

Thank you for the input. The change is implemented and merged into the hotfix batch. It will be further tested and released soon