Search code examples
javascriptamazon-web-serviceselectronaws-amplify

AWS Amplify Authentication


Hoping someone can help me please, I can't find what I'm looking for online...I'm using AWS Amplify sign in and looking for a way to determine if the user is already signed in and if so, kick the other sessions and only allow the current sign-in session. I know I can use the global sign out, and if that's the case do I do it after the signing (as there is no parameter for use only the global tag) and if so, won't that also expire the token of the user just signed in?

Does anyone have any suggestions? Thank you in advance!

Edit: I have it working that if the user is already logged in then I use Global Sign Out and re log back in for the current user. Now on the other computers that were logged in, an hour later the token expires and doesn't get refreshed (this is what I wanted) but my app freezes. Is there anyway I can catch the error for expired token and use that to close my app?

Many thanks again!


Solution

  • For anyone looking at something similar, you can watch Amplify events with the following functions: https://docs.amplify.aws/lib/auth/auth-events/q/platform/js

    import { Hub, Logger } from 'aws-amplify';
    
    const logger = new Logger('My-Logger');
    
    const listener = (data) => {
        switch (data.payload.event) {
            case 'signIn':
                logger.info('user signed in');
                break;
            case 'signUp':
                logger.info('user signed up');
                break;
            case 'signOut':
                logger.info('user signed out');
                break;
            case 'signIn_failure':
                logger.error('user sign in failed');
                break;
            case 'tokenRefresh':
                logger.info('token refresh succeeded');
                break;
            case 'tokenRefresh_failure':
                logger.error('token refresh failed');
                break;
            case 'configured':
                logger.info('the Auth module is configured');
        }
    }
    
    Hub.listen('auth', listener);