Home > Software design >  Creating different User Types in AWS Amplify
Creating different User Types in AWS Amplify

Time:05-07

I am planning to use AWS Amplify as a backend for a mobile application. The App consists of two User Types (UserTypeA,UserTypeB). They have some common data points and some unique one's too.

UserTypeA(id, email, firstName, lastName, profilePicture, someUniquePropertyForUserTypeA) UserTypeB(id, email, firstName, lastName, profilePicture, someUniquePropertyForUserTypeB)

What would be a scalable approach to achieve this? I am also using AWS Amplify authentication so I can save the common data as CustomAttributes offered by Cognito, but then how would I save the uniqueProperties for the two user types. Will this approach scale?

This is a social app and is heavily reliant on other users' profile data as well (which will be queried most of the time).

CodePudding user response:

This is a Database design problem. To solve this, you can try creating a relation that has the common attributes in it, that is, User with attributes, (ID, email, firstName, lastName, profilePicture, someUniquePropertyForUserTypeA).

After that, create sub-classed based relations, that is UserTypeA, and UserTypeB.

These relations will have a unique ID, and have a foreign key relation with the parent (User). How? The first major relation would be 'User'. The 2 sub classed relations would be 'UserTypeA', and 'UserTypeB'.

The 'User' has an attribute 'ID'.

So the two sub classes have an attribute, 'User_ID', which is a foregin relation to 'User'.'ID'.

Now just autogen another ID column for UserTypeA and UserTypeB.

This way, you have a central table which has a unique ID for all users, and then you have a unique ID in each of the sub class relations, which together with User_ID forms a composite key.

CodePudding user response:

Check out the patterns that are recommended by AppSync (The graphQL service that is behind Amplify when adding graphQL API). It is described in detail here: https://docs.aws.amazon.com/appsync/latest/devguide/security-authorization-use-cases.html

The main idea is to have multiple user pools defined in Cognito and then you can use the different groups in the resolvers. For example:

// This checks if the user is part of the Admin group and makes the call
#foreach($group in $context.identity.claims.get("cognito:groups"))
    #if($group == "Admin")
        #set($inCognitoGroup = true)
    #end
#end
#if($inCognitoGroup)
{
    "version" : "2017-02-28",
    "operation" : "UpdateItem",
    "key" : {
        "id" : $util.dynamodb.toDynamoDBJson($ctx.args.id)
    },
    "attributeValues" : {
        "owner" : $util.dynamodb.toDynamoDBJson($context.identity.username)
        #foreach( $entry in $context.arguments.entrySet() )
            ,"${entry.key}" : $util.dynamodb.toDynamoDBJson($entry.value)
        #end
    }
}
#else
    $utils.unauthorized()
#end

or using the @directives on the graphQL schema, such as:

type Query {
   posts:[Post!]!
   @aws_auth(cognito_groups: ["Bloggers", "Readers"])
}

type Mutation {
   addPost(id:ID!, title:String!):Post!
   @aws_auth(cognito_groups: ["Bloggers"])
}
...
  • Related