Search code examples
amazon-cognitoaws-amplify

Admin and super admin Authorization and conditional rendering using AWS Amplify + React app


I am working on Amplify + React fullstack app and have created complete Authentication flow. I want to authorize users based on their roles i.e. admin, superadmin, user, etc. When I sign up a new user, I am seeing the details in AWS Cognito but I am not able to figure out the roles for the user. I have two solutions in mind.

  1. Add a userType custom attribute in User table in AWS Cognito.
  2. Create three different groups in the pool and handle them manually.

I want to know which solution is better. I am unable to find any help online so it would be great if the detailed steps are shared. Thanks in advance.


Solution

  • cognito groups are meant for exact same use cases. you can create a group named ADMIN and in your client you may check if the signed in user is part of ADMIN group like this

    user.signInUserSession.accessToken.payload["cognito:groups"]?.includes("ADMIN")