Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add user to own database during registration in a 'transactional' way?

I am trying to use AWS Cognito for my app.

I can register the users successfully to AWS Cognito, but I want to be able to also store them in my database (with my own API), too, as part of the sign-up process.

I saw that I could use a trigger function like Post confirmation, but as far as I understand, if saving the user to my database fails, then the user will still be created in the Cognito user pool.

I want to treat the sign-up process in a transactional way, so that if saving the user in my database fails, then the entire process fails.

So, it would look something like this:

  Aws Cognito Sign Up -> Send_to_database_trigger ----fails----> user is not created 
                                                  \---success--> user is created

Is there any way to achieve what I want? Thanks.

like image 765
Toma Radu-Petrescu Avatar asked Nov 06 '22 18:11

Toma Radu-Petrescu


1 Answers

You can use the Pre Sign-up Lambda Trigger.

It gets the username and all the user attributes in the event and if it fails the user won't be signed up in Cognito.

In your case, you will save your user to your database in this trigger (after other possible validations) and will fail if saving to your database has failed. This way you can be sure that if a user exists in Cognito it also exists in your database.

If you need to save the sub then you can implement both Pre Sign-up and Post Confirmation and update your database record with the sub in the Post Confirmation trigger.

like image 127
jogold Avatar answered Nov 12 '22 13:11

jogold