Skip to content

Commit 1ceb3ef

Browse files
committed
fix(aws-amplify-react): only call Auth signOut if some error happens during signed in
1 parent 1bd5684 commit 1ceb3ef

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/aws-amplify-react/src/Auth/Authenticator.jsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import AmplifyMessageMap from '../AmplifyMessageMap';
3131
import { Container, Toast } from '../Amplify-UI/Amplify-UI-Components-React';
3232

3333
const logger = new Logger('Authenticator');
34+
const AUTHENTICATOR_AUTHSTATE = 'amplify-authenticator-authState';
3435

3536
export default class Authenticator extends Component {
3637
constructor(props) {
@@ -75,7 +76,17 @@ export default class Authenticator extends Component {
7576
})
7677
.catch(err => {
7778
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+
});
7990
});
8091
}
8192

@@ -91,6 +102,11 @@ export default class Authenticator extends Component {
91102
if (state === this.state.auth) { return; }
92103

93104
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+
}
94110
this.setState({ auth: state, authData: data, error: null, showToast: false });
95111
if (this.props.onStateChange) { this.props.onStateChange(state, data); }
96112
}

0 commit comments

Comments
 (0)