'AWS Amplify PubSub is keeping previous user subscription active even after SignOut

Unfortunately i discovered a faulty behavior of AWS Amplify PubSub.

Lets say user A has the authority to subscribe through PubSub library (got it's permission by manually attaching iot policy for the specific user's Cognito identity id). He is receiving successfully messages from AWS IoT and it's time for him to SignOut (without unsubscribe first) for an other user to SignIn from the same computer.

So user B SignIn but he doesn't have authority to subscribe (no policy has been attached to his Cognito identity id). Unfortunately Amplify will keep the subscription active from the previous user and the Non-authorized user B will receive the mqtt data successfully until he reload the page. Of course the opposite stands too (user A can't recieve cause previously logged in user B didn't had authority to subscribe)

I managed to solve it by running unsubscribe function on componentWillUnmount() but i'm not satisfied with this approach.

Do you think i'm doing something wrong? I'm subscribing with

PubSub.subscribe('myTopic').subscribe({
    next: data => console.log('Message received', data),
    error: error => console.error(error),
    complete: () => console.log('Done'),
});

on redux-action and simply dispatch the message on next: data =>



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source