https://avo.cool logo
We store most of our user info in our database ...
# avo-2
m
Thread automatically created by kadams54 in #740893011994738751
b
My first pass in the `user_controller.rb`:
Copy code
ruby
class Avo::UsersController < Avo::ResourcesController
  around_action :create_cognito_user, only: :create

  private

  def create_cognito_user
    ActiveRecord::Base.transaction do
      yield
      user = @resource.model
      cognito_user = cognito_client.create_user(user)
      user.update_column(:cognito_id, cognito_user.user.username)
    end
  end

  def cognito_client
    @cognito_client ||= CognitoClient.new
  end
end
This approach sorta worked… when the DB operation was successful, my Cognito user was created. When there was a DB failure—like dupe email addresses—the controller would roll back the transaction and prevent the user from being created in Cognito. The problem is that this approach also mucks up the
create_fail_action
so we get an error page instead of the nice flash/turbo notification.
l
Wdyt about after / before action on
create_fail_action
and / or
create_success_action
?
I mean you can do something like
Copy code
ruby
def create_success_action
  # some_logic_here
  super
end
l
here are the docs for that
please ensure you run
super
b
I'll give it a whirl; why I didn't initially go that route is because I wanted the DB transaction to be rolled back if the AWS Cognito operation failed. That said, transactions have their own tradeoffs. I've been pondering other options to sort of heal situations where the DB User doesn't have its AWS Cognito counterpart.
l
I think you could initiate a transaction there too
Let us know how it goes