@@ -31,6 +31,7 @@ import AmplifyMessageMap from '../AmplifyMessageMap';
31
31
import { Container , Toast } from '../Amplify-UI/Amplify-UI-Components-React' ;
32
32
33
33
const logger = new Logger ( 'Authenticator' ) ;
34
+ const AUTHENTICATOR_AUTHSTATE = 'amplify-authenticator-authState' ;
34
35
35
36
export default class Authenticator extends Component {
36
37
constructor ( props ) {
@@ -75,7 +76,17 @@ export default class Authenticator extends Component {
75
76
} )
76
77
. catch ( err => {
77
78
if ( ! this . _isMounted ) { return ; }
78
- Auth . signOut ( ) . then ( ( ) => this . handleStateChange ( this . _initialAuthState ) ) ;
79
+ let cachedAuthState = null ;
80
+ try {
81
+ cachedAuthState = localStorage . getItem ( AUTHENTICATOR_AUTHSTATE ) ;
82
+ } catch ( e ) {
83
+ logger . debug ( 'Failed to get the auth state from local storage' , e ) ;
84
+ }
85
+ const promise = cachedAuthState === 'signedIn' ? Auth . signOut ( ) : Promise . resolve ( ) ;
86
+ promise . then ( ( ) => this . handleStateChange ( this . _initialAuthState ) )
87
+ . catch ( ( e ) => {
88
+ logger . debug ( 'Failed to sign out' , e ) ;
89
+ } ) ;
79
90
} ) ;
80
91
}
81
92
@@ -91,6 +102,11 @@ export default class Authenticator extends Component {
91
102
if ( state === this . state . auth ) { return ; }
92
103
93
104
if ( state === 'signedOut' ) { state = 'signIn' ; }
105
+ try {
106
+ localStorage . setItem ( AUTHENTICATOR_AUTHSTATE , state ) ;
107
+ } catch ( e ) {
108
+ logger . debug ( 'Failed to set the auth state into local storage' , e ) ;
109
+ }
94
110
this . setState ( { auth : state , authData : data , error : null , showToast : false } ) ;
95
111
if ( this . props . onStateChange ) { this . props . onStateChange ( state , data ) ; }
96
112
}
0 commit comments