AWS Amplify – List the currently logged in users

Step 1: Install the graphql-ttl-transformer module

npm install --save graphql-ttl-transformer

Step 2: Update the transform.conf.json to include the graphql-ttl-transformer entry

{
    "Version": 5,
    "ElasticsearchWarning": true,
    "transformers": [
        "graphql-ttl-transformer"
    ]
}

Step 3: Update the schema.graphql file to create a new table to maintain the list of currently logged in users

type CurrentLoggedInUsers @model {
  id: ID!
  email: String
  expTime: AWSTimestamp! @ttl
}

Step 4: Make sure a record is added to this table with the expTime of 30 minutes into the future. Also, make sure the same record is updated every minutes as long as the user is active on the site.

Step 5: The following FE code will get you the list of all the users that are currently logged in.

const userLoginItems = await API.graphql(
    graphqlOperation(listCurrentLoggedInUsers)
);

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *