Doing user management the right way is hard work. Allowing users to register themselves, and verifying their identity can be difficult. However, using Amazon Cognito and the Vonage Verify API help make it a bit easier by doing the heavy lifting.
With a few web forms, Vonage SDK, and AWS SDK, you can create a standard user management system allowing a user to register, update their credentials, validate their identity, and login. You’ll use the code from the nexmo-community/verify-aws-cognito-php code repo.
- PHP 7.4 (update
serverless.ymlfor other versions)
- Composer installed globally
- AWS account
- Vonage API account
Vonage API Account
To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.
Clone the nexmo-community/verify-aws-cognito-php repo from GitHub, and navigate into the newly created directory to proceed.
This example requires the use of Composer to install dependencies and set up the autoloader.
Assuming you have Composer installed globally, run:
This example uses Amazon Cognito User Pools to hold users. I set up a User Pool as follows:
- Navigate to the Amazon Cognito Dashboard in the AWS Console.
Manage User Pools.
- Create a new user pool.
- Give the pool a name, and click
Step through settings
Email address or phone numberand pick
Allow email addresses
- Set the minimum password length, and desired complexity settings
- Make sure to
Allow users to sign themselves up
- Leave the next step as-is, for this example. We will use Vonage for 2FA
- Select a
FROM email address ARNfrom the dropdown. This assumes you've already created an idenity in Amazon Simple Email Service(SES)
- Add a
FROM email addressas desired.
- Leave the rest of this page unchanged. However, I do recommend you remove the trailing periods from the email messages. This prevents the recipient from mistakenly using the period as part of the temporary password.
- Skip adding tags by clicking
- Skip devices by clicking
- Click the link to
Add an app client
- Give the app client a name
- Uncheck the box to
Generate client secret
- Check the rest of the boxes
Create app client
- Skip triggers by clicking
Rename the provided
.env.default file to
.env and update the values as needed:
NOTE: All placeholders noted by
<> need to be updated. Update the others as needed.
Launch or Deploy
Test the app by running it locally with the PHP built-in webserver with the command:
View the main landing page by going to
http://localhost:8080 in a web browser.
IMPORTANT: Though this app functions, it is intended for educational purposes and is not ready for public use as-is.
The app flow is as follows:
- After registration (
user_register.php) the user is redirected to a mandatory password change page. (
login_reset.php) Here they should utilize the temporary password emailed to them.
- After updating the temporary password the user is redirected to the login page. (
- The login is where a new user, or an existing user can login.
- After successful login, the user is redirected to the 2FA verification page. (
login_verify.php) Here they enter the 6 digit code sent to their mobile number.
- Upon successful 2FA verification, the user is then redirected back to the main page (
index.php) where they see they are now logged in with an option to logout. (
Originally published at https://www.nexmo.com/blog/2020/07/10/aws-cognito-verify-with-php